1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
Crypt::Spritz - Crypt::CBC compliant Spritz encryption/hash/mac/aead/prng module |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
|
|
use Crypt::Spritz; |
8 |
|
|
|
9 |
|
|
# keysize() is 32, but spritz accepts any key size |
10 |
|
|
# blocksize() is 16, but cna be anything |
11 |
|
|
|
12 |
|
|
$cipher = new Crypt::Twofish2 "a" x 32, Crypt::Twofish2::MODE_CBC; |
13 |
|
|
|
14 |
|
|
$crypted = $cipher->encrypt($plaintext); |
15 |
|
|
# - OR - |
16 |
|
|
$plaintext = $cipher->decrypt($crypted); |
17 |
|
|
|
18 |
|
|
=head1 DESCRIPTION |
19 |
|
|
|
20 |
|
|
This module implements the spritz spongelike function. |
21 |
|
|
|
22 |
|
|
Although it is C<Crypt::CBC> compliant you usually gain nothing by using |
23 |
|
|
that module (except generality, which is often a good thing), since |
24 |
|
|
C<Crypt::Twofish2> can work in either ECB or CBC mode itself. |
25 |
|
|
|
26 |
|
|
=over 4 |
27 |
|
|
|
28 |
|
|
=cut |
29 |
|
|
|
30 |
|
|
package Crypt::Spritz; |
31 |
|
|
|
32 |
|
|
use XSLoader; |
33 |
|
|
|
34 |
|
|
$VERSION = '0.0'; |
35 |
|
|
|
36 |
|
|
XSLoader::load __PACKAGE__, $VERSION; |
37 |
|
|
|
38 |
|
|
@Crypt::Spritz::CipherBase::ISA = |
39 |
root |
1.2 |
@Crypt::Spritz::Hash::ISA = |
40 |
root |
1.1 |
@Crypt::Spritz::PRNG::ISA = Crypt::Spritz::; |
41 |
|
|
|
42 |
root |
1.2 |
@Crypt::Spritz::MAC::ISA = Crypt::Spritz::Hash::; |
43 |
root |
1.1 |
|
44 |
root |
1.2 |
@Crypt::Spritz::Cipher::XOR::ISA = |
45 |
|
|
@Crypt::Spritz::Cipher::ISA = |
46 |
root |
1.1 |
@Crypt::Spritz::AEAD::ISA = |
47 |
|
|
@Crypt::Spritz::AEAD::XOR::ISA = Crypt::Spritz::CipherBase::; |
48 |
|
|
|
49 |
|
|
sub Crypt::Spritz::CipherBase::keysize () { 32 } |
50 |
|
|
sub Crypt::Spritz::CipherBase::blocksize () { 64 } |
51 |
|
|
|
52 |
root |
1.2 |
*Crypt::Spritz::Hash::add = |
53 |
root |
1.1 |
*Crypt::Spritz::PRNG::add = \&Crypt::Spritz::absorb; |
54 |
|
|
|
55 |
|
|
*Crypt::Spritz::PRNG::get = \&Crypt::Spritz::squeeze; |
56 |
|
|
|
57 |
|
|
*Crypt::Spritz::AEAD::XOR::finish = |
58 |
root |
1.2 |
*Crypt::Spritz::AEAD::finish = \&Crypt::Spritz::Hash::finish; |
59 |
root |
1.1 |
|
60 |
|
|
*Crypt::Spritz::AEAD::XOR::associated_data = |
61 |
|
|
*Crypt::Spritz::AEAD::associated_data = |
62 |
|
|
*Crypt::Spritz::AEAD::XOR::nonce = |
63 |
|
|
*Crypt::Spritz::AEAD::nonce = \&Crypt::Spritz::absborb_and_stop; |
64 |
|
|
|
65 |
|
|
=item keysize |
66 |
|
|
|
67 |
|
|
Returns the keysize, which is 32 (bytes). The Twofish2 cipher actually |
68 |
|
|
supports keylengths of 16, 24 or 32 bytes, but there is no way to |
69 |
|
|
communicate this to C<Crypt::CBC>. |
70 |
|
|
|
71 |
|
|
=item blocksize |
72 |
|
|
|
73 |
|
|
The blocksize for Twofish2 is 16 bytes (128 bits), which is somewhat |
74 |
|
|
unique. It is also the reason I need this module myself ;) |
75 |
|
|
|
76 |
|
|
=item $cipher = new $key [, $mode] |
77 |
|
|
|
78 |
|
|
Create a new C<Crypt::Twofish2> cipher object with the given key (which |
79 |
|
|
must be 128, 192 or 256 bits long). The additional C<$mode> argument is |
80 |
|
|
the encryption mode, either C<MODE_ECB> (electronic cookbook mode, the |
81 |
|
|
default), C<MODE_CBC> (cipher block chaining, the same that C<Crypt::CBC> |
82 |
|
|
does) or C<MODE_CFB1> (1-bit cipher feedback mode). |
83 |
|
|
|
84 |
|
|
ECB mode is very insecure (read a book on cryptography if you don't know |
85 |
|
|
why!), so you should probably use CBC mode. CFB1 mode is not tested and is |
86 |
|
|
most probably broken, so do not try to use it. |
87 |
|
|
|
88 |
|
|
In ECB mode you can use the same cipher object to encrypt and decrypt |
89 |
|
|
data. However, every change of "direction" causes an internal reordering |
90 |
|
|
of key data, which is quite slow, so if you want ECB mode and |
91 |
|
|
encryption/decryption at the same time you should create two seperate |
92 |
|
|
C<Crypt::Twofish2> objects with the same key. |
93 |
|
|
|
94 |
|
|
In CBC mode you have to use seperate objects for encryption/decryption in |
95 |
|
|
any case. |
96 |
|
|
|
97 |
|
|
The C<MODE_*>-constants are not exported by this module, so you must |
98 |
|
|
specify them as C<Crypt::Twofish2::MODE_CBC> etc. (sorry for that). |
99 |
|
|
|
100 |
|
|
=item $cipher->encrypt($data) |
101 |
|
|
|
102 |
|
|
Encrypt data. The size of C<$data> must be a multiple of C<blocksize> (16 |
103 |
|
|
bytes), otherwise this function will croak. Apart from that, it can be of |
104 |
|
|
(almost) any length. |
105 |
|
|
|
106 |
|
|
=item $cipher->decrypt($data) |
107 |
|
|
|
108 |
|
|
The pendant to C<encrypt> in that it I<de>crypts data again. |
109 |
|
|
|
110 |
|
|
=back |
111 |
|
|
|
112 |
|
|
=head1 SEE ALSO |
113 |
|
|
|
114 |
|
|
L<Crypt::CBC>, L<Digest::HMAC>, L<http://people.csail.mit.edu/rivest/pubs/RS14.pdf>. |
115 |
|
|
|
116 |
|
|
=head1 SECURITY CONSIDERATIONS |
117 |
|
|
|
118 |
|
|
I also cannot guarantee for security. |
119 |
|
|
|
120 |
|
|
=head1 AUTHOR |
121 |
|
|
|
122 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
123 |
|
|
http://home.schmorp.de/ |
124 |
|
|
|
125 |
|
|
The actual twofish encryption is written in horribly microsoft'ish looking |
126 |
|
|
almost ansi-c by Doug Whiting. |
127 |
|
|
|
128 |
|
|
=cut |
129 |
|
|
|
130 |
|
|
1; |
131 |
|
|
|