1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Crypt::Spritz - Spritz stream cipher/hash/MAC/AEAD/CSPRNG module |
3 | Crypt::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 | |
… | |
… | |
46 | slower than RC4 or AES, hashing many times slower than SHA-3, although |
46 | slower than RC4 or AES, hashing many times slower than SHA-3, although |
47 | this might be reversed on an 8-bit-cpu) and the fact that it is totally |
47 | this might be reversed on an 8-bit-cpu) and the fact that it is totally |
48 | unproven in the field (as of this writing, the cipher was just a few |
48 | unproven in the field (as of this writing, the cipher was just a few |
49 | months old), so it can't be called production-ready. |
49 | months old), so it can't be called production-ready. |
50 | |
50 | |
51 | All the usual caveats regarding stream ciphers apply - never repeat |
51 | All the usual caveats regarding stream ciphers apply - never repeat your |
52 | your key, never repeat your nonce and so on - you should have some basic |
52 | key, never repeat your nonce and so on - you should have some basic |
53 | understanding of cryptography before using this cipher in your own |
53 | understanding of cryptography before using this cipher in your own |
54 | designs. |
54 | designs. |
55 | |
55 | |
56 | The Spritz base class is not meant for end users. To make usage simpler |
56 | The Spritz base class is not meant for end users. To make usage simpler |
57 | and safer, a number of convenience classes are provided for typical |
57 | and safer, a number of convenience classes are provided for typical |
… | |
… | |
67 | |
67 | |
68 | package Crypt::Spritz; |
68 | package Crypt::Spritz; |
69 | |
69 | |
70 | use XSLoader; |
70 | use XSLoader; |
71 | |
71 | |
72 | $VERSION = '0.0'; |
72 | $VERSION = '0.1'; |
73 | |
73 | |
74 | XSLoader::load __PACKAGE__, $VERSION; |
74 | XSLoader::load __PACKAGE__, $VERSION; |
75 | |
75 | |
76 | @Crypt::Spritz::ISA = Crypt::Spritz::Base::; |
76 | @Crypt::Spritz::ISA = Crypt::Spritz::Base::; |
77 | |
77 | |
… | |
… | |
120 | |
120 | |
121 | =item $spritz->init # InitializeState |
121 | =item $spritz->init # InitializeState |
122 | |
122 | |
123 | Initialises the Spritz state again, throwing away the previous state. |
123 | Initialises the Spritz state again, throwing away the previous state. |
124 | |
124 | |
|
|
125 | =item $another_spritz = $spritz->clone |
|
|
126 | |
|
|
127 | Make an exact copy of the spritz state. This method can be called on all |
|
|
128 | of the objects in this module, but is documented separately to give some |
|
|
129 | cool usage examples. |
|
|
130 | |
125 | =item $spritz->update # Update |
131 | =item $spritz->update # Update |
126 | |
132 | |
127 | =item $spritz->whip ($r) # Whip |
133 | =item $spritz->whip ($r) # Whip |
128 | |
134 | |
129 | =item $spritz->crush # Crush |
135 | =item $spritz->crush # Crush |
… | |
… | |
135 | Calls the Spritz primitive ovf the same name - these are not normally |
141 | Calls the Spritz primitive ovf the same name - these are not normally |
136 | called manually. |
142 | called manually. |
137 | |
143 | |
138 | =item $spritz->absorb ($I) # Absorb |
144 | =item $spritz->absorb ($I) # Absorb |
139 | |
145 | |
140 | Absorbs the given data into the state (usually used for key material, nonces, IVs |
146 | Absorbs the given data into the state (usually used for key material, |
141 | messages to be hashed and so on). |
147 | nonces, IVs messages to be hashed and so on). |
142 | |
148 | |
143 | =item $spritz->absorb_stop # AbsorbStop |
149 | =item $spritz->absorb_stop # AbsorbStop |
144 | |
150 | |
145 | Absorbs a special stop symbol - this is usually used as delimiter between |
151 | Absorbs a special stop symbol - this is usually used as delimiter between |
146 | multiple strings to be absorbed, to thwart extension attacks. |
152 | multiple strings to be absorbed, to thwart extension attacks. |
… | |
… | |
207 | |
213 | |
208 | =item $encrypted = $cipher->crypt ($cleartext) |
214 | =item $encrypted = $cipher->crypt ($cleartext) |
209 | |
215 | |
210 | =item $cleartext = $cipher->crypt ($encrypted) |
216 | =item $cleartext = $cipher->crypt ($encrypted) |
211 | |
217 | |
212 | Encrypt or decrypt a piece of a message. This cna be called as many times |
218 | Encrypt or decrypt a piece of a message. This can be called as many times |
213 | as you want, and the message can be split into as few or many pieces as |
219 | as you want, and the message can be split into as few or many pieces as |
214 | required without affecting the results. |
220 | required without affecting the results. |
215 | |
221 | |
216 | =item $cipher->crypt_inplace ($cleartext_or_ciphertext) |
222 | =item $cipher->crypt_inplace ($cleartext_or_ciphertext) |
217 | |
223 | |
218 | Same as C<crypt>, except it I<modifies the argument in-place>. |
224 | Same as C<crypt>, except it I<modifies the argument in-place>. |
|
|
225 | |
|
|
226 | =item $another_cipher = $cipher->clone |
|
|
227 | |
|
|
228 | Make an exact copy of the cipher state. This can be useful to cache states |
|
|
229 | for reuse later, for example, to avoid expensive key setups. |
|
|
230 | |
|
|
231 | While there might be use cases for this feature, it makes a lot more sense |
|
|
232 | for C<Crypt::Spritz::AEAD> and C<Crypt::Spritz::AEAD::XOR>, as they allow |
|
|
233 | you to specify the IV/nonce separately. |
219 | |
234 | |
220 | =item $constant_32 = $cipher->keysize |
235 | =item $constant_32 = $cipher->keysize |
221 | |
236 | |
222 | =item $constant_64 = $cipher->blocksize |
237 | =item $constant_64 = $cipher->blocksize |
223 | |
238 | |
… | |
… | |
265 | cannot sensibly be used for further hashing afterwards. |
280 | cannot sensibly be used for further hashing afterwards. |
266 | |
281 | |
267 | Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit |
282 | Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit |
268 | digests, respectively. |
283 | digests, respectively. |
269 | |
284 | |
|
|
285 | =item $another_hasher = $hasher->clone |
|
|
286 | |
|
|
287 | Make an exact copy of the hasher state. This can be useful to generate |
|
|
288 | incremental hashes, for example. |
|
|
289 | |
|
|
290 | Example: generate a hash for the data already fed into the hasher, by keeping |
|
|
291 | the original hasher for further C<add> calls and calling C<finish> on a C<clone>. |
|
|
292 | |
|
|
293 | my $intermediate_hash = $hasher->clone->finish; |
|
|
294 | |
|
|
295 | Example: hash 64KiB of data, and generate a hash after every kilobyte that |
|
|
296 | is 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 | |
|
|
309 | These kind of intermediate hashes are sometimes used in communications |
|
|
310 | protocols to protect the integrity of the data incrementally, e.g. to |
|
|
311 | detect errors early, while still having a complete hash at the end of a |
|
|
312 | transfer. |
|
|
313 | |
270 | =back |
314 | =back |
271 | |
315 | |
272 | |
316 | |
273 | =head2 THE Crypt::Spritz::MAC CLASS |
317 | =head2 THE Crypt::Spritz::MAC CLASS |
274 | |
318 | |
… | |
… | |
312 | Calculates a message code of the given length and return it. The object |
356 | Calculates a message code of the given length and return it. The object |
313 | cannot sensibly be used for further hashing afterwards. |
357 | cannot sensibly be used for further hashing afterwards. |
314 | |
358 | |
315 | Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit |
359 | Typical digest lengths are 16 and 32, corresponding to 128 and 256 bit |
316 | digests, respectively. |
360 | digests, respectively. |
|
|
361 | |
|
|
362 | =item $another_hasher = $hasher->clone |
|
|
363 | |
|
|
364 | Make an exact copy of the hasher state. This can be useful to |
|
|
365 | generate incremental macs, for example. |
|
|
366 | |
|
|
367 | See the description for the C<Crypt::Spritz::Hash::clone> method for some |
|
|
368 | examples. |
317 | |
369 | |
318 | =back |
370 | =back |
319 | |
371 | |
320 | |
372 | |
321 | =head2 THE Crypt::Spritz::AEAD::XOR CLASS |
373 | =head2 THE Crypt::Spritz::AEAD::XOR CLASS |
… | |
… | |
400 | increments, and thus reuse the same sequence number. The problem with |
452 | increments, and thus reuse the same sequence number. The problem with |
401 | random strings i that your random number generator might be hosed and |
453 | random strings i that your random number generator might be hosed and |
402 | generate the same randomness multiple times (randomness can be very hard |
454 | generate the same randomness multiple times (randomness can be very hard |
403 | to get especially on embedded devices). |
455 | to get especially on embedded devices). |
404 | |
456 | |
405 | =item $aead->associated_data ($data)( |
457 | =item $aead->associated_data ($data) |
406 | |
458 | |
407 | Provide the associated data (cleartext data to be authenticated but not |
459 | Provide the associated data (cleartext data to be authenticated but not |
408 | encrypted). This method I<must> be called I<after> C<nonce> and I<before> |
460 | encrypted). This method I<must> be called I<after> C<nonce> and I<before> |
409 | C<crypt>. |
461 | C<crypt>. |
410 | |
462 | |
… | |
… | |
419 | |
471 | |
420 | =item $encrypted = $cipher->crypt ($cleartext) |
472 | =item $encrypted = $cipher->crypt ($cleartext) |
421 | |
473 | |
422 | =item $cleartext = $cipher->crypt ($encrypted) |
474 | =item $cleartext = $cipher->crypt ($encrypted) |
423 | |
475 | |
424 | Encrypt or decrypt a piece of a message. This cna be called as many times |
476 | Encrypt or decrypt a piece of a message. This can be called as many times |
425 | as you want, and the message can be split into as few or many pieces as |
477 | as you want, and the message can be split into as few or many pieces as |
426 | required without affecting the results, with one exception: All except the |
478 | required without affecting the results, with one exception: All except the |
427 | last call to C<crypt> needs to pass in a multiple of C<64> octets. The |
479 | last call to C<crypt> needs to pass in a multiple of C<64> octets. The |
428 | last call to C<crypt> does not have this limitation. |
480 | last call to C<crypt> does not have this limitation. |
429 | |
481 | |
430 | =item $cipher->crypt_inplace ($cleartext_or_ciphertext) |
482 | =item $cipher->crypt_inplace ($cleartext_or_ciphertext) |
431 | |
483 | |
432 | Same as C<crypt>, except it I<modifies the argument in-place>. |
484 | Same as C<crypt>, except it I<modifies the argument in-place>. |
|
|
485 | |
|
|
486 | =item $another_cipher = $cipher->clone |
|
|
487 | |
|
|
488 | Make an exact copy of the cipher state. This can be useful to cache states |
|
|
489 | for reuse later, for example, to avoid expensive key setups. |
|
|
490 | |
|
|
491 | Example: set up a cipher state with a key, then clone and use it to |
|
|
492 | encrypt 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 | } |
433 | |
505 | |
434 | =back |
506 | =back |
435 | |
507 | |
436 | |
508 | |
437 | =head2 THE Crypt::Spritz::PRNG CLASS |
509 | =head2 THE Crypt::Spritz::PRNG CLASS |