ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Crypt-Spritz/Spritz.pm
(Generate patch)

Comparing Crypt-Spritz/Spritz.pm (file contents):
Revision 1.4 by root, Sat Jan 10 07:10:46 2015 UTC vs.
Revision 1.6 by root, Sat Jan 10 07:48:29 2015 UTC

1=head1 NAME 1=head1 NAME
2 2
3Crypt::Spritz - Crypt::CBC compliant Spritz encryption/hash/mac/aead/prng module 3Crypt::Spritz - Spritz stream cipher/hash/MAC/AEAD/CSPRNG family
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Crypt::Spritz; 7 use Crypt::Spritz;
8 8
9 # keysize() is 32, but spritz accepts any key size 9 # see the commented examples in their respective classes,
10 # blocksize() is 16, but cna be anything 10 # but basically
11 11
12 $cipher = new Crypt::Twofish2 "a" x 32, Crypt::Twofish2::MODE_CBC; 12 my $cipher = new Crypt::Spritz::Cipher::XOR $key, $iv;
13
14 $crypted = $cipher->encrypt($plaintext); 13 $ciphertext = $cipher->crypt ($cleartext);
15 # - OR - 14
16 $plaintext = $cipher->decrypt($crypted); 15 my $hasher = new Crypt::Spritz::Hash;
16 $hasher->add ($data);
17 $digest = $hasher->finish;
18
19 my $hasher = new Crypt::Spritz::MAC $key;
20 $hasher->add ($data);
21 $mac = $hasher->finish;
22
23 my $aead = new Crypt::Spritz::AEAD::XOR $key;
24 $aead->nonce ($counter);
25 $aead->associated_data ($header);
26 $ciphertext = $aead->crypt ($cleartext);
27 $mac = $aead->mac;
28
29 my $prng = new Crypt::Spritz::PRNG $entropy;
30 $prng->add ($additional_entropy);
31 $keydata = $prng->get (32);
17 32
18=head1 DESCRIPTION 33=head1 DESCRIPTION
19 34
20This module implements the Spritz spongelike function (with N=256), the 35This module implements the Spritz spongelike function (with N=256), the
21spiritual successor of RC4 developed by Ron Rivest and Jacob Schuldt. 36spiritual successor of RC4 developed by Ron Rivest and Jacob Schuldt.
31slower than RC4 or AES, hashing many times slower than SHA-3, although 46slower than RC4 or AES, hashing many times slower than SHA-3, although
32this might be reversed on an 8-bit-cpu) and the fact that it is totally 47this might be reversed on an 8-bit-cpu) and the fact that it is totally
33unproven in the field (as of this writing, the cipher was just a few 48unproven in the field (as of this writing, the cipher was just a few
34months old), so it can't be called production-ready. 49months old), so it can't be called production-ready.
35 50
36All the usual caveats regarding stream ciphers apply - never repeat 51All the usual caveats regarding stream ciphers apply - never repeat your
37your key, never repeat your nonce etc. - you should have some basic 52key, never repeat your nonce and so on - you should have some basic
38understanding of cryptography before using this cipher in your own 53understanding of cryptography before using this cipher in your own
39designs. 54designs.
40 55
41The Spritz base class is not meant for end users. To make usage simpler 56The Spritz base class is not meant for end users. To make usage simpler
42and safer, a number of convenience classes are provided for typical 57and safer, a number of convenience classes are provided for typical
52 67
53package Crypt::Spritz; 68package Crypt::Spritz;
54 69
55use XSLoader; 70use XSLoader;
56 71
57$VERSION = '0.0'; 72$VERSION = '0.1';
58 73
59XSLoader::load __PACKAGE__, $VERSION; 74XSLoader::load __PACKAGE__, $VERSION;
60 75
61@Crypt::Spritz::ISA = Crypt::Spritz::Base::; 76@Crypt::Spritz::ISA = Crypt::Spritz::Base::;
62 77
105 120
106=item $spritz->init # InitializeState 121=item $spritz->init # InitializeState
107 122
108Initialises the Spritz state again, throwing away the previous state. 123Initialises the Spritz state again, throwing away the previous state.
109 124
125=item $another_spritz = $spritz->clone
126
127Make an exact copy of the spritz state. This method can be called on all
128of the objects in this module, but is documented separately to give some
129cool usage examples.
130
110=item $spritz->update # Update 131=item $spritz->update # Update
111 132
112=item $spritz->whip ($r) # Whip 133=item $spritz->whip ($r) # Whip
113 134
114=item $spritz->crush # Crush 135=item $spritz->crush # Crush
120Calls the Spritz primitive ovf the same name - these are not normally 141Calls the Spritz primitive ovf the same name - these are not normally
121called manually. 142called manually.
122 143
123=item $spritz->absorb ($I) # Absorb 144=item $spritz->absorb ($I) # Absorb
124 145
125Absorbs the given data into the state (usually used for key material, nonces, IVs 146Absorbs the given data into the state (usually used for key material,
126messages to be hashed and so on). 147nonces, IVs messages to be hashed and so on).
127 148
128=item $spritz->absorb_stop # AbsorbStop 149=item $spritz->absorb_stop # AbsorbStop
129 150
130Absorbs a special stop symbol - this is usually used as delimiter between 151Absorbs a special stop symbol - this is usually used as delimiter between
131multiple strings to be absorbed, to thwart extension attacks. 152multiple strings to be absorbed, to thwart extension attacks.
192 213
193=item $encrypted = $cipher->crypt ($cleartext) 214=item $encrypted = $cipher->crypt ($cleartext)
194 215
195=item $cleartext = $cipher->crypt ($encrypted) 216=item $cleartext = $cipher->crypt ($encrypted)
196 217
197Encrypt or decrypt a piece of a message. This cna be called as many times 218Encrypt or decrypt a piece of a message. This can be called as many times
198as you want, and the message can be split into as few or many pieces as 219as you want, and the message can be split into as few or many pieces as
199required without affecting the results. 220required without affecting the results.
200 221
201=item $cipher->crypt_inplace ($cleartext_or_ciphertext) 222=item $cipher->crypt_inplace ($cleartext_or_ciphertext)
202 223
203Same as C<crypt>, except it I<modifies the argument in-place>. 224Same as C<crypt>, except it I<modifies the argument in-place>.
225
226=item $another_cipher = $cipher->clone
227
228Make an exact copy of the cipher state. This can be useful to cache states
229for reuse later, for example, to avoid expensive key setups.
230
231While there might be use cases for this feature, it makes a lot more sense
232for C<Crypt::Spritz::AEAD> and C<Crypt::Spritz::AEAD::XOR>, as they allow
233you to specify the IV/nonce separately.
204 234
205=item $constant_32 = $cipher->keysize 235=item $constant_32 = $cipher->keysize
206 236
207=item $constant_64 = $cipher->blocksize 237=item $constant_64 = $cipher->blocksize
208 238
250cannot sensibly be used for further hashing afterwards. 280cannot sensibly be used for further hashing afterwards.
251 281
252Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit 282Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit
253digests, respectively. 283digests, respectively.
254 284
285=item $another_hasher = $hasher->clone
286
287Make an exact copy of the hasher state. This can be useful to generate
288incremental hashes, for example.
289
290Example: generate a hash for the data already fed into the hasher, by keeping
291the original hasher for further C<add> calls and calling C<finish> on a C<clone>.
292
293 my $intermediate_hash = $hasher->clone->finish;
294
295Example: hash 64KiB of data, and generate a hash after every kilobyte that
296is over the full data.
297
298 my $hasher = new Crypt::Spritz::Hash;
299
300 for (0..63) {
301 my $kib = "x" x 1024; # whatever data
302
303 $hasher->add ($kib);
304
305 my $intermediate_hash = $hasher->clone->finish;
306 ...
307 }
308
309These kind of intermediate hashes are sometimes used in communications
310protocols to protect the integrity of the data incrementally, e.g. to
311detect errors early, while still having a complete hash at the end of a
312transfer.
313
255=back 314=back
256 315
257 316
258=head2 THE Crypt::Spritz::MAC CLASS 317=head2 THE Crypt::Spritz::MAC CLASS
259 318
297Calculates a message code of the given length and return it. The object 356Calculates a message code of the given length and return it. The object
298cannot sensibly be used for further hashing afterwards. 357cannot sensibly be used for further hashing afterwards.
299 358
300Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit 359Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit
301digests, respectively. 360digests, respectively.
361
362=item $another_hasher = $hasher->clone
363
364Make an exact copy of the hasher state. This can be useful to
365generate incremental macs, for example.
366
367See the description for the C<Crypt::Spritz::Hash::clone> method for some
368examples.
302 369
303=back 370=back
304 371
305 372
306=head2 THE Crypt::Spritz::AEAD::XOR CLASS 373=head2 THE Crypt::Spritz::AEAD::XOR CLASS
385increments, and thus reuse the same sequence number. The problem with 452increments, and thus reuse the same sequence number. The problem with
386random strings i that your random number generator might be hosed and 453random strings i that your random number generator might be hosed and
387generate the same randomness multiple times (randomness can be very hard 454generate the same randomness multiple times (randomness can be very hard
388to get especially on embedded devices). 455to get especially on embedded devices).
389 456
390=item $aead->associated_data ($data)( 457=item $aead->associated_data ($data)
391 458
392Provide the associated data (cleartext data to be authenticated but not 459Provide the associated data (cleartext data to be authenticated but not
393encrypted). This method I<must> be called I<after> C<nonce> and I<before> 460encrypted). This method I<must> be called I<after> C<nonce> and I<before>
394C<crypt>. 461C<crypt>.
395 462
404 471
405=item $encrypted = $cipher->crypt ($cleartext) 472=item $encrypted = $cipher->crypt ($cleartext)
406 473
407=item $cleartext = $cipher->crypt ($encrypted) 474=item $cleartext = $cipher->crypt ($encrypted)
408 475
409Encrypt or decrypt a piece of a message. This cna be called as many times 476Encrypt or decrypt a piece of a message. This can be called as many times
410as you want, and the message can be split into as few or many pieces as 477as you want, and the message can be split into as few or many pieces as
411required without affecting the results, with one exception: All except the 478required without affecting the results, with one exception: All except the
412last call to C<crypt> needs to pass in a multiple of C<64> octets. The 479last call to C<crypt> needs to pass in a multiple of C<64> octets. The
413last call to C<crypt> does not have this limitation. 480last call to C<crypt> does not have this limitation.
414 481
415=item $cipher->crypt_inplace ($cleartext_or_ciphertext) 482=item $cipher->crypt_inplace ($cleartext_or_ciphertext)
416 483
417Same as C<crypt>, except it I<modifies the argument in-place>. 484Same as C<crypt>, except it I<modifies the argument in-place>.
485
486=item $another_cipher = $cipher->clone
487
488Make an exact copy of the cipher state. This can be useful to cache states
489for reuse later, for example, to avoid expensive key setups.
490
491Example: set up a cipher state with a key, then clone and use it to
492encrypt messages with different nonces.
493
494 my $cipher = new Crypt::Spritz::AEAD::XOR $key;
495
496 my $message_counter;
497
498 for my $message ("a", "b", "c") {
499 my $clone = $cipher->clone;
500 $clone->nonce (pack "N", ++$message_counter);
501 $clone->associated_data ("");
502 my $encrypted = $clone->crypt ($message);
503 ...
504 }
418 505
419=back 506=back
420 507
421 508
422=head2 THE Crypt::Spritz::PRNG CLASS 509=head2 THE Crypt::Spritz::PRNG CLASS
495completely unproven. 582completely unproven.
496 583
497=head1 AUTHOR 584=head1 AUTHOR
498 585
499 Marc Lehmann <schmorp@schmorp.de> 586 Marc Lehmann <schmorp@schmorp.de>
500 http://home.schmorp.de/ 587 http://software.schmorp.de/pkg/Crypt-Spritz
501 588
502=cut 589=cut
503 590
5041; 5911;
505 592

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines