Debian Codegraph @

lib/Crypt/Util.pmc

raw
1 # Generated by tt 0.02 (Module::Compile 0.20) - do not edit!
2 ################((( 32-bit Checksum Validator III )))################
3 #line 1
4 BEGIN { use 5.006; local (*F, $/); ($F = __FILE__) =~ s!c$!!; open(F)
5 or die "Cannot open $F: $!"; binmode(F, ':crlf'); if (unpack('%32N*',
6 $F=readline(*F)) != 0x47C88156) { use Filter::Util::Call; my $f = $F;
7 filter_add(sub { filter_del(); 1 while &filter_read; $_ = $f; 1; })}}
8 #line 1
9 # 8667e12ea286715fb3e93c82b3356305890112f1
10
11 package Crypt::Util;
12 use Moose;
13
14 our $VERSION = "0.11";
15
16 use Digest;
17 use Digest::MoreFallbacks;
18
19 use Carp qw/croak/;
20
21 use Sub::Exporter;
22 use Data::OptList;
23
24 use namespace::clean -except => [qw(meta)];
25
26 our %DEFAULT_ACCESSORS = (
27 mode => { isa => "Str" },
28 authenticated_mode => { isa => "Str" },
29 encode => { isa => "Bool" },
30 encoding => { isa => "Str" },
31 printable_encoding => { isa => "Str" },
32 alphanumerical_encoding => { isa => "Str" },
33 uri_encoding => { isa => "Str" },
34 digest => { isa => "Str" },
35 cipher => { isa => "Str" },
36 mac => { isa => "Str" },
37 key => { isa => "Str" },
38 uri_encoding => { isa => "Str" },
39 printable_encoding => { isa => "Str" },
40 use_literal_key => { isa => "Bool" },
41 tamper_proof_unencrypted => { isa => "Bool" },
42 nonce => { isa => "Str", default => "" },
43 );
44
45 our @DEFAULT_ACCESSORS = keys %DEFAULT_ACCESSORS;
46
47 my %export_groups = (
48 'crypt' => [qw/
49 encrypt_string decrypt_string
50 authenticated_encrypt_string
51 tamper_proof thaw_tamper_proof
52 cipher_object
53 /],
54 digest => [qw/
55 digest_string verify_hash verify_digest
56 digest_object
57 mac_digest_string
58 verify_mac
59 /],
60 encoding => [qw/
61 encode_string decode_string
62 encode_string_hex decode_string_hex
63 encode_string_base64 decode_string_base64 encode_string_base64_wrapped
64 encode_string_base32 decode_string_base32
65 encode_string_uri_base64 decode_string_uri_base64
66 encode_string_uri decode_string_uri
67 encode_string_alphanumerical decode_string_alphanumerical
68 encode_string_printable decode_string_printable
69 encode_string_uri_escape decode_string_uri_escape
70 /],
71 params => [ "exported_instance", "disable_fallback", map { "default_$_" } @DEFAULT_ACCESSORS ],
72 );
73
74 my %exports = map { $_ => \&__curry_instance } map { @$_ } values %export_groups;
75
76 Sub::Exporter->import( -setup => {
77 exports => \%exports,
78 groups => \%export_groups,
79 collectors => {
80 defaults => sub { 1 },
81 },
82 });
83
84 our @KNOWN_AUTHENTICATING_MODES = qw(EAX OCB GCM CWC CCM); # IACBC & IAPM will probably never be implemented
85
86 our %KNOWN_AUTHENTICATING_MODES = map { $_ => 1 } @KNOWN_AUTHENTICATING_MODES;
87
88 our %FALLBACK_LISTS = (
89 mode => [qw/CFB CBC Ctr OFB/],
90 stream_mode => [qw/CFB Ctr OFB/],
91 block_mode => [qw/CBC/],
92 authenticated_mode => [qw/EAX GCM CCM/], # OCB/], OCB is patented
93 cipher => [qw/Rijndael Serpent Twofish RC6 Blowfish RC5/],
94 # fa884d2582f6f05a5ffc6634115076027e7e91e1
95 digest => [qw/SHA-1 SHA-256 RIPEMD160 Whirlpool MD5 Haval256/],
96 mac => [qw/HMAC CMAC/],
97 encoding => [qw/hex/],
98 printable_encoding => [qw/base64 hex/],
99 alphanumerical_encoding => [qw/base32 hex/],
100 uri_encoding => [qw/uri_base64 base32 hex/],
101 );
102
103 foreach my $fallback ( keys %FALLBACK_LISTS ) {
104 my @list = @{ $FALLBACK_LISTS{$fallback} };
105
106 my $list_method = "fallback_${fallback}_list";
107
108 my $list_method_sub = sub { # derefed list accessors
109 my ( $self, @args ) = @_;
110 if ( @args ) {
111 @args = @{ $args[0] } if @args == 1 and (ref($args[0])||'') eq "ARRAY";
112 $self->{$list_method} = \@args;
113 }
114 @{ $self->{$list_method} || \@list };
115 };
116
117 my $type = ( $fallback =~ /(encoding|mode)/ )[0] || $fallback;
118 my $try = "_try_${type}_fallback";
119
120 my $fallback_sub = sub {
121 my $self = shift;
122
123 $self->_find_fallback(
124 $fallback,
125 $try,
126 $self->$list_method,
127 ) || croak "Couldn't load any $fallback";
128 };
129
130 no strict 'refs';
131 *{ "fallback_$fallback" } = $fallback_sub;
132 *{ $list_method } = $list_method_sub;
133 }
134
135 foreach my $attr ( @DEFAULT_ACCESSORS ) {
136 has "default_$attr" => (
137 is => "rw",
138 predicate => "has_default_$attr",
139 clearer => "clear_default_$attr",
140 ( __PACKAGE__->can("fallback_$attr") ? (
141 lazy_build => 1,
142 builder => "fallback_$attr",
143 ) : () ),
144 %{ $DEFAULT_ACCESSORS{$attr} },
145 );
146 }
147
148 has disable_fallback => (
149 isa => "Bool",
150 is => "rw",
151 );
152
153 __PACKAGE__->meta->make_immutable if __PACKAGE__->meta->can("make_immutable");
154
155 {
156 my %fallback_caches;
157
158 sub _find_fallback {
159 my ( $self, $key, $test, @list ) = @_;
160
161 my $cache = $fallback_caches{$key} ||= {};
162
163 @list = $list[0] if @list and $self->disable_fallback;
164
165 foreach my $elem ( @list ) {
166 $cache->{$elem} = $self->$test( $elem ) unless exists $cache->{$elem};
167 return $elem if $cache->{$elem};
168 }
169
170
171 return;
172 }
173 }
174
175 sub _try_cipher_fallback {
176 my ( $self, $name ) = @_;
177 $self->_try_loading_module("Crypt::$name");
178 }
179
180 sub _try_digest_fallback {
181 my ( $self, $name ) = @_;
182
183 my $e;
184
185 {
186 local $@;
187 eval { $self->digest_object( digest => $name ) };
188 $e = $@;
189 };
190
191 return 1 if !$e;
192 ( my $file = $name ) =~ s{::}{/}g;
193 die $e if $e !~ m{^Can't locate Digest/\Q${file}.pm\E in \@INC};
194 return;
195 }
196
197 sub _try_mode_fallback {
198 my ( $self, $mode ) = @_;
199 $self->_try_loading_module("Crypt::$mode");
200 }
201
202 sub _try_mac_fallback {
203 my ( $self, $mac ) = @_;
204 $self->_try_loading_module("Digest::$mac");
205 }
206
207 sub _try_loading_module {
208 my ( $self, $name ) = @_;
209
210 (my $file = "${name}.pm") =~ s{::}{/}g;
211
212 my ( $r, $e );
213 {
214 local $@;
215 $r = eval { require $file }; # yes it's portable
216 $e = $@;
217 };
218
219 return $r if $r;
220 die $e if $e !~ /^Can't locate \Q$file\E in \@INC/;
221 return $r;
222 }
223
224 {
225 my %encoding_module = (
226 base64 => "MIME::Base64",
227 uri_base64 => "MIME::Base64::URLSafe",
228 base32 => "MIME::Base32",
229 uri_escape => "URI::Escape",
230 );
231
232 sub _try_encoding_fallback {
233 my ( $self, $encoding ) = @_;
234
235 return 1 if $encoding eq "hex";
236
237 my $module = $encoding_module{$encoding};
238 $module =~ s{::}{/}g;
239 $module .= ".pm";
240
241 my $e = do {
242 local $@;
243 eval { require $module }; # yes it's portable
244 $@;
245 };
246
247 return 1 if !$e;
248 die $e if $e !~ /^Can't locate \Q$module\E in \@INC/;
249 return;
250 }
251 }
252
253 sub _args (\@;$) {
254 my ( $args, $odd ) = @_;
255
256 my ( $self, @args ) = @$args;
257
258 my %params;
259 if ( @args % 2 == 1 ) {
260 croak "The parameters must be an even sized list of key value pairs" unless defined $odd;
261 ( my $odd_value, %params ) = @args;
262 croak "Can't provide the positional param in the named list as well" if exists $params{$odd};
263 $params{$odd} = $odd_value;
264 } else {
265 %params = @args;
266 }
267
268 return ( $self, %params );
269 }
270
271 sub _process_params {
272 my ( $self, $params, @required ) = @_;
273
274 foreach my $param ( @required ) {
275 next if exists $params->{$param};
276
277 $params->{$param} = $self->_process_param( $param );
278 }
279 }
280
281 sub _process_param {
282 my ( $self, $param ) = @_;
283
284 my $default = "default_$param";
285
286 if ( $self->can($default) ) {
287 return $self->$default;
288 }
289
290 croak "No default value for required parameter '$param'";
291 }
292
293 sub cipher_object {
294 my ( $self, %params ) = _args @_;
295
296 $self->_process_params( \%params, qw/mode/);
297
298 my $method = "cipher_object_" . lc(my $mode = delete $params{mode});
299
300 croak "mode $mode is unsupported" unless $self->can($method);
301
302 $self->$method( %params );
303 }
304
305 sub cipher_object_eax {
306 my ( $self, %params ) = _args @_;
307
308 $self->_process_params( \%params, qw/cipher nonce/ );
309
310 require Crypt::EAX;
311
312 Crypt::EAX->new(
313 %params,
314 cipher => "Crypt::$params{cipher}", # FIXME take a ref, but Crypt::CFB will barf
315 key => $self->process_key(%params),
316 nonce => $params{nonce},
317 );
318 }
319
320 sub cipher_object_cbc {
321 my ( $self, %params ) = _args @_;
322
323 $self->_process_params( \%params, qw/cipher/ );
324
325 require Crypt::CBC;
326
327 Crypt::CBC->new(
328 -cipher => $params{cipher},
329 -key => $self->process_key(%params),
330 );
331 }
332
333 sub cipher_object_ofb {
334 my ( $self, %params ) = _args @_;
335
336 $self->_process_params( \%params, qw/cipher/ );
337
338 require Crypt::OFB;
339 my $c = Crypt::OFB->new;
340
341 $c->padding( Crypt::ECB::PADDING_AUTO() );
342
343 $c->key( $self->process_key(%params) );
344
345 $c->cipher( $params{cipher} );
346
347 return $c;
348 }
349
350 sub cipher_object_cfb {
351 my ( $self, @args ) = _args @_;
352 require Crypt::CFB;
353 $self->_cipher_object_baurem( "Crypt::CFB", @args );
354 }
355
356 sub cipher_object_ctr {
357 my ( $self, @args ) = _args @_;
358 require Crypt::Ctr;
359 $self->_cipher_object_baurem( "Crypt::Ctr", @args );
360 }
361
362 sub _cipher_object_baurem {
363 my ( $self, $class, %params ) = @_;
364
365 my $prefix = "Crypt";
366 ( $prefix, $params{cipher} ) = ( Digest => delete $params{digest} ) if exists $params{encryption_digest};
367
368 $self->_process_params( \%params, qw/cipher/ );
369
370 $class->new( $self->process_key(%params), join("::", $prefix, $params{cipher}) );
371 }
372
373
374 sub cipher_object_stream {
375 my ( $self, @args ) = _args @_;
376 my $mode = $self->_process_param("stream_mode");
377 $self->cipher_object( @args, mode => $mode );
378 }
379
380 sub cipher_object_block {
381 my ( $self, @args ) = _args @_;
382 my $mode = $self->_process_param("block_mode");
383 $self->cipher_object( @args, mode => $mode );
384 }
385
386 sub cipher_object_authenticated {
387 my ( $self, @args ) = _args @_;
388 my $mode = $self->_process_param("authenticated_mode");
389 $self->cipher_object( @args, mode => $mode );
390 }
391
392
393 sub process_nonce {
394 my ( $self, %params ) = _args @_, 'nonce';
395
396 my $nonce = $self->_process_params( \%params, 'nonce' );
397
398 if ( length($nonce) ) {
399 return $nonce;
400 } else {
401 require Data::GUID;
402 Data::GUID->new->as_binary;
403 }
404 }
405
406 sub process_key {
407 my ( $self, %params ) = _args @_, "key";
408
409 if ( $params{literal_key} || $self->default_use_literal_key ) {
410 $self->_process_params( \%params, qw/key/ );
411 return $params{key};
412 } else {
413 my $size = $params{key_size};
414
415 unless ( $size ) {
416 $self->_process_params( \%params, qw/key cipher/ );
417 my $cipher = $params{cipher};
418
419 my $class = "Crypt::$cipher";
420
421 $self->_try_loading_module($class);
422
423 if ( my $size_method = $class->can("keysize") || $class->can("blocksize") ) {
424 $size = $class->$size_method;
425 }
426
427 $size ||= $cipher eq "Blowfish" ? 56 : 32;
428 }
429
430 return $self->digest_string(
431 string => $params{key},
432 digest => "MultiHash",
433 encode => 0,
434 digest_args => [{
435 width => $size,
436 hashes => ["SHA-512"], # no need to be overkill, we just need the variable width
437 }],
438 );
439 }
440 }
441
442 sub digest_object {
443 my ( $self, %params ) = _args @_;
444
445 $self->_process_params( \%params, qw/
446 digest
447 /);
448
449 Digest->new( $params{digest}, @{ $params{digest_args} || [] } );
450 }
451
452 {
453 # c423c07872e6e8002ee9c826c602c9e5e9ec705a
454
455 package
456 Crypt::Util::HMACDigestFactory;
457
458 sub new {
459 my $self = shift;
460 $$self->clone;
461 }
462
463 sub new_factory {
464 my ( $self, $thing ) = @_;
465 return bless \$thing, $self;
466 }
467 }
468
469 sub mac_object {
470 my ( $self, %params ) = _args @_;
471
472 $self->_process_params( \%params, qw/
473 mac
474 /);
475
476 my $mac_type = delete $params{mac};
477
478 my $method = lc( "mac_object_$mac_type" );
479
480 $self->$method( %params );
481 }
482
483 sub mac_object_hmac {
484 my ( $self, @args ) = _args @_;
485
486 my $digest = $self->digest_object(@args);
487
488 my $digest_factory = Crypt::Util::HMACDigestFactory->new_factory( $digest );
489
490 my $key = $self->process_key(
491 literal_key => 1, # Digest::HMAC does it's own key processing, but we let the user force our own
492 key_size => 64, # if the user did force our own, the default key_size is Digest::HMAC's default block size
493 @args,
494 );
495
496 require Digest::HMAC;
497 Digest::HMAC->new(
498 $key,
499 $digest_factory,
500 # 8f6ca5a32f36f2311a153d671093109b46db764e
501 );
502 }
503
504 sub mac_object_cmac {
505 my ( $self, %params ) = _args @_;
506
507 my ( $key, $cipher );
508
509 if ( ref $params{cipher} ) {
510 $cipher = $params{cipher};
511 } else {
512 $self->_process_params( \%params, qw(cipher) );
513 $cipher = "Crypt::" . $params{cipher};
514 $key = $self->process_key(%params);
515 }
516
517 require Digest::CMAC;
518 Digest::CMAC->new( $key, $cipher );
519 }
520
521
522 sub encrypt_string {
523 my ( $self, %params ) = _args @_, "string";
524
525 my $string = delete $params{string};
526 croak "You must provide the 'string' parameter" unless defined $string;
527
528 my $c = $self->cipher_object( %params );
529
530
531 $self->maybe_encode( $c->encrypt($string), \%params );
532
533 }
534
535 sub maybe_encode {
536 my ( $self, $string, $params ) = @_;
537
538 my $should_encode = exists $params->{encode}
539 ? $params->{encode}
540 : exists $params->{encoding} || $self->default_encode;
541
542 if ( $should_encode ) {
543 return $self->encode_string(
544 %$params,
545 string => $string,
546 );
547 } else {
548 return $string;
549 }
550 }
551
552 sub decrypt_string {
553 my ( $self, %params ) = _args @_, "string";
554
555 my $string = delete $params{string};
556 croak "You must provide the 'string' parameter" unless defined $string;
557
558 my $c = $self->cipher_object( %params );
559
560
561 $c->decrypt( $self->maybe_decode($string, \%params ) );
562
563 }
564
565 sub maybe_decode {
566 my ( $self, $string, $params ) = @_;
567
568 my $should_encode = exists $params->{decode}
569 ? $params->{decode}
570 : exists $params->{encoding} || $self->default_encode;
571
572 if ( $should_encode ) {
573 return $self->decode_string(
574 %$params,
575 string => $string,
576 );
577 } else {
578 return $string;
579 }
580 }
581
582
583 sub _digest_string_with_object {
584 my ( $self, $object, %params ) = @_;
585
586 my $string = delete $params{string};
587 croak "You must provide the 'string' parameter" unless defined $string;
588
589 $object->add($string);
590
591 $self->maybe_encode( $object->digest, \%params );
592 }
593
594 sub digest_string {
595 my ( $self, %params ) = _args @_, "string";
596
597 my $d = $self->digest_object( %params );
598
599 $self->_digest_string_with_object( $d, %params );
600 }
601
602 sub mac_digest_string {
603 my ( $self, %params ) = _args @_, "string";
604
605 my $d = $self->mac_object( %params );
606
607 $self->_digest_string_with_object( $d, %params );
608 }
609
610 sub _do_verify_hash {
611 my ( $self, %params ) = _args @_;
612
613 my $hash = delete $params{hash};
614 my $fatal = delete $params{fatal};
615 croak "You must provide the 'string' and 'hash' parameters" unless defined $params{string} and defined $hash;
616
617 my $meth = $params{digest_method};
618
619 return 1 if $hash eq $self->$meth(%params);
620
621 if ( $fatal ) {
622 croak "Digest verification failed";
623 } else {
624 return;
625 }
626 }
627
628 sub verify_hash {
629 my ( $self, @args ) = @_;
630 $self->_do_verify_hash(@args, digest_method => "digest_string");
631 }
632
633 sub verify_digest {
634 my ( $self, @args ) = @_;
635 $self->verify_hash(@args);
636 }
637
638 sub verify_mac {
639 my ( $self, @args ) = @_;
640 $self->_do_verify_hash(@args, digest_method => "mac_digest_string");
641 }
642
643 {
644 my @flags = qw/serialized/;
645
646 sub _flag_hash_to_int {
647 my ( $self, $flags ) = @_;
648
649 my $bit = 1;
650 my $flags_int = 0;
651
652 foreach my $flag (@flags) {
653 $flags_int |= $bit if $flags->{$flag};
654 } continue {
655 $bit *= 2;
656 }
657
658 return $flags_int;
659 }
660
661 sub _flag_int_to_hash {
662 my ( $self, $flags ) = @_;
663
664 my $bit =1;
665 my %flags;
666
667 foreach my $flag (@flags ) {
668 $flags{$flag} = $flags & $bit;
669 } continue {
670 $bit *= 2;
671 }
672
673 return wantarray ? %flags : \%flags;
674 }
675 }
676
677 sub tamper_proof {
678 my ( $self, %params ) = _args @_, "data";
679
680 $params{string} = $self->pack_data( %params );
681 # 1f109116c3860903aa093cd9aa88faff4b02e021
682
683 $self->tamper_proof_string( %params );
684 }
685
686 sub freeze_data {
687 my ( $self, %params ) = @_;
688 require Storable;
689 Storable::nfreeze($params{data});
690 }
691
692 sub thaw_data {
693 my ( $self, %params ) = @_;
694 require Storable;
695 Storable::thaw($params{data});
696 }
697
698 sub tamper_proof_string {
699 my ( $self, %params ) = _args @_, "string";
700
701 my $encrypted = exists $params{encrypt}
702 ? $params{encrypt}
703 : !$self->default_tamper_proof_unencrypted;
704
705
706 my $type = ( $encrypted ? "aead" : "mac" );
707
708 my $method = "${type}_tamper_proof_string";
709
710 my $string = $self->$method( %params );
711
712 return $self->_pack_tamper_proof( $type => $string );
713 }
714
715 {
716 my @tamper_proof_types = qw/mac aead/;
717 my %tamper_proof_type; @tamper_proof_type{@tamper_proof_types} = 1 .. @tamper_proof_types;
718
719 sub _pack_tamper_proof {
720 my ( $self, $type, $proof ) = @_;
721 pack("C a*", $tamper_proof_type{$type}, $proof);
722 }
723
724 sub _unpack_tamper_proof {
725 my ( $self, $packed ) = @_;
726 my ( $type, $string ) = unpack("C a*", $packed);
727
728 return (
729 ($tamper_proof_types[ $type-1 ] || croak "Unknown tamper proofing method"),
730 $string,
731 );
732 }
733 }
734
735 sub _authenticated_mode {
736 my ( $self, $params ) = @_;
737
738 # d8ca7f81e88c105a34bbdfeca3f345ae6b895ab4
739 if ( exists $params->{authenticated_mode} ) {
740 $params->{mode} = delete $params->{authenticated_mode};
741 return 1;
742 }
743
744 # 0f84e32ca8a994cc47b73f87ebe2255970458187
745 if ( exists $params->{mode} ) {
746 # 90064c4d2aeb8499c4f9c733a18ef416b8d667bf
747 if ( exists $params->{mode_is_authenticated} ) {
748 return $params->{mode_is_authenticated};
749 }
750
751 if ( $KNOWN_AUTHENTICATING_MODES{uc($params->{mode})} ) {
752 return 1;
753 } else {
754 return;
755 }
756 }
757
758 $params->{mode} = $self->_process_param('authenticated_mode');
759
760 return 1;
761 }
762
763 sub _pack_hash_and_message {
764 my ( $self, $hash, $message ) = @_;
765 pack("n/a* a*", $hash, $message);
766 }
767
768 sub _unpack_hash_and_message {
769 my ( $self, $packed ) = @_;
770 unpack("n/a* a*", $packed);
771 }
772
773 our $PACK_FORMAT_VERSION = 1;
774
775 sub pack_data {
776 my ( $self, %params ) = _args @_, "data";
777
778 $self->_process_params( \%params, qw/
779 data
780 /);
781
782 my $data = delete $params{data};
783
784 my %flags;
785
786 if ( ref $data ) {
787 $flags{serialized} = 1;
788 $data = $self->freeze_data( %params, data => $data );
789 }
790
791 $self->_pack_version_flags_and_string( $PACK_FORMAT_VERSION, \%flags, $data );
792 }
793
794 sub unpack_data {
795 my ( $self, %params ) = _args @_, "data";
796
797 $self->_process_params( \%params, qw/
798 data
799 /);
800
801 my ( $version, $flags, $data ) = $self->_unpack_version_flags_and_string($params{data});
802
803 $self->_packed_string_version_check( $version );
804
805 if ( $flags->{serialized} ) {
806 return $self->thaw_data( %params, data => $data );
807 } else {
808 return $data;
809 }
810 }
811
812 sub _pack_version_flags_and_string {
813 my ( $self, $version, $flags, $string ) = @_;
814 pack("n n N/a*", $version, $self->_flag_hash_to_int($flags), $string);
815 }
816
817 sub _unpack_version_flags_and_string {
818 my ( $self, $packed ) = @_;
819
820 my ( $version, $flags, $string ) = unpack("n n N/a*", $packed);
821
822 $flags = $self->_flag_int_to_hash($flags);
823
824 return ( $version, $flags, $string );
825 }
826
827 sub authenticated_encrypt_string {
828 my ( $self, %params ) = _args @_, "string";
829
830 # 0d10af4c2d455ed7ab4f4524fbbd8fb8a81e3c6b
831 if ( $self->_authenticated_mode(\%params) ) {
832 $self->_process_params( \%params, qw(nonce) );
833
834 # d338aed66b1330e2c735ee56ba65fead37360041
835 my $nonce = $self->process_nonce(%params); # FIMXE limit to 64k?
836
837 # 24b22e09d92227262d2f8edb54dbae1559118a51
838
839 return pack("n/a* a*", $nonce, $self->encrypt_string( %params, nonce => $nonce ) );
840 } else {
841 croak "To use encrypted tamper resistent strings an authenticated encryption mode such as EAX must be selected";
842 }
843 }
844
845 sub authenticated_decrypt_string {
846 my ( $self, %params ) = _args @_, "string";
847
848 if ( $self->_authenticated_mode(\%params) ) {
849 $self->_process_params( \%params, qw(string) );
850
851 my ( $nonce, $string ) = unpack("n/a* a*", $params{string});
852
853 return $self->decrypt_string(
854 fatal => 1,
855 %params,
856 nonce => $nonce,
857 string => $string,
858 );
859 } else {
860 croak "To use encrypted tamper resistent strings an authenticated encryption mode such as EAX must be selected";
861 }
862 }
863
864 sub aead_tamper_proof_string {
865 my ( $self, %params ) = _args @_, "string";
866
867 $self->authenticated_encrypt_string( %params );
868 }
869
870 sub mac_tamper_proof_string {
871 my ( $self, %params ) = _args @_, "string";
872
873 my $string = delete $params{string};
874 croak "You must provide the 'string' parameter" unless defined $string;
875
876 my $hash = $self->mac_digest_string(
877 %params,
878 encode => 0,
879 string => $string,
880 );
881
882 return $self->_pack_hash_and_message( $hash, $string );
883 }
884
885 sub thaw_tamper_proof_string {
886 my ( $self, %params ) = _args @_, "string";
887
888 my $string = delete $params{string};
889 croak "You must provide the 'string' parameter" unless defined $string;
890
891 my ( $type, $message ) = $self->_unpack_tamper_proof($string);
892
893 my $method = "thaw_tamper_proof_string_$type";
894
895 my $packed = $self->$method( %params, string => $message );
896 }
897
898 sub thaw_tamper_proof {
899 my ( $self, %params ) = _args @_, "string";
900 my $packed = $self->thaw_tamper_proof_string(%params);
901 $self->unpack_data(%params, data => $packed);
902 }
903
904 sub thaw_tamper_proof_string_aead {
905 my ( $self, %params ) = _args @_, "string";
906
907 $self->authenticated_decrypt_string( %params );
908 }
909
910 sub thaw_tamper_proof_string_mac {
911 my ( $self, %params ) = _args @_, "string";
912
913 my $hashed_packed = delete $params{string};
914 croak "You must provide the 'string' parameter" unless defined $hashed_packed;
915
916 my ( $hash, $packed ) = $self->_unpack_hash_and_message( $hashed_packed );
917
918 return unless $self->verify_mac(
919 fatal => 1,
920 %params,
921 hash => $hash,
922 decode => 0,
923 string => $packed,
924 );
925
926 return $packed;
927 }
928
929 sub _packed_string_version_check {
930 my ( $self, $version ) = @_;
931
932 croak "Incompatible packed string (I'm version $PACK_FORMAT_VERSION, thawing version $version)"
933 unless $version == $PACK_FORMAT_VERSION;
934 }
935
936
937 sub encode_string {
938 my ( $self, %params ) = _args @_, "string";
939
940 my $string = delete $params{string};
941 croak "You must provide the 'string' parameter" unless defined $string;
942
943 $self->_process_params( \%params, qw/
944 encoding
945 /);
946
947 my $encoding = delete $params{encoding};
948 croak "Encoding method must be an encoding name" unless $encoding;
949 my $method = "encode_string_$encoding";
950 croak "Encoding method $encoding is not supported" unless $self->can($method);
951
952 $self->$method($string);
953 }
954
955 sub decode_string {
956 my ( $self, %params ) = _args @_, "string";
957
958 my $string = delete $params{string};
959 croak "You must provide the 'string' parameter" unless defined $string;
960
961 $self->_process_params( \%params, qw/
962 encoding
963 /);
964
965 my $encoding = delete $params{encoding};
966 croak "Encoding method must be an encoding name" unless $encoding;
967 my $method = "decode_string_$encoding";
968 croak "Encoding method $encoding is not supported" unless $self->can($method);
969
970 $self->$method($string);
971 }
972
973
974 sub encode_string_hex {
975 my ( $self, $string ) = @_;
976 unpack("H*", $string);
977 }
978
979 sub decode_string_hex {
980 my ( $self, $hex ) = @_;
981 pack("H*", $hex );
982 }
983
984 sub encode_string_base64 {
985 my ( $self, $string ) = @_;
986 require MIME::Base64;
987 MIME::Base64::encode_base64($string, "");
988 }
989
990 sub encode_string_base64_wrapped {
991 my ( $self, $string ) = @_;
992 require MIME::Base64;
993 MIME::Base64::encode_base64($string);
994 }
995
996 sub decode_string_base64 {
997 my ( $self, $base64 ) = @_;
998 require MIME::Base64;
999 MIME::Base64::decode_base64($base64);
1000 }
1001
1002 # 246d37740a5c4c6a218f83be4355351b9fb8bdbd
1003 sub encode_string_uri_base64 {
1004 my ( $self, $string ) = @_;
1005 require MIME::Base64::URLSafe;
1006 MIME::Base64::URLSafe::encode($string);
1007 }
1008
1009 sub decode_string_uri_base64 {
1010 my ( $self, $base64 ) = @_;
1011 require MIME::Base64::URLSafe;
1012 MIME::Base64::URLSafe::decode($base64);
1013 }
1014
1015 sub encode_string_base32 {
1016 my ( $self, $string ) = @_;
1017 require MIME::Base32;
1018 MIME::Base32::encode_rfc3548($string);
1019 }
1020
1021 sub decode_string_base32 {
1022 my ( $self, $base32 ) = @_;
1023 require MIME::Base32;
1024 MIME::Base32::decode_rfc3548(uc($base32));
1025 }
1026
1027 sub encode_string_uri_escape {
1028 my ( $self, $string ) = @_;
1029 require URI::Escape;
1030 URI::Escape::uri_escape($string);
1031 }
1032
1033 sub decode_string_uri_escape {
1034 my ( $self, $uri_escaped ) = @_;
1035 require URI::Escape;
1036 URI::Escape::uri_unescape($uri_escaped);
1037 }
1038
1039
1040
1041 sub encode_string_uri {
1042 my ( $self, $string ) = @_;
1043 my $encoding = $self->_process_param("uri_encoding");
1044 $self->encode_string( string => $string, encoding => $encoding );
1045 }
1046
1047 sub decode_string_uri {
1048 my ( $self, $string ) = @_;
1049 my $encoding = $self->_process_param("uri_encoding");
1050 $self->decode_string( string => $string, encoding => $encoding );
1051 }
1052
1053
1054
1055 sub encode_string_alphanumerical {
1056 my ( $self, $string ) = @_;
1057 my $encoding = $self->_process_param("alphanumerical_encoding");
1058 $self->encode_string( string => $string, encoding => $encoding );
1059 }
1060
1061 sub decode_string_alphanumerical {
1062 my ( $self, $string ) = @_;
1063 my $encoding = $self->_process_param("alphanumerical_encoding");
1064 $self->decode_string( string => $string, encoding => $encoding );
1065 }
1066
1067
1068
1069 sub encode_string_printable {
1070 my ( $self, $string ) = @_;
1071 my $encoding = $self->_process_param("printable_encoding");
1072 $self->encode_string( string => $string, encoding => $encoding );
1073 }
1074
1075 sub decode_string_printable {
1076 my ( $self, $string ) = @_;
1077 my $encoding = $self->_process_param("printable_encoding");
1078 $self->decode_string( string => $string, encoding => $encoding );
1079 }
1080
1081
1082
1083 sub exported_instance {
1084 my $self = shift;
1085 return $self;
1086 }
1087
1088 sub __curry_instance {
1089 my ($class, $method_name, undef, $col) = @_;
1090
1091 my $self = $col->{instance} ||= $class->__curry_flavoured_instance($col);
1092
1093 sub { $self->$method_name(@_) };
1094 }
1095
1096 sub __curry_flavoured_instance {
1097 my ( $class, $col ) = @_;
1098
1099 my %params; @params{ map { "default_$_" } keys %{ $col->{defaults} } } = values %{ $col->{defaults} };
1100
1101 $class->new( \%params );
1102 }
1103
1104 __PACKAGE__;
1105
1106 __END__
1107
1108 =pod
1109
1110 =head1 NAME
1111
1112 Crypt::Util - A lightweight Crypt/Digest convenience API
1113
1114 =head1 SYNOPSIS
1115
1116 use Crypt::Util; # also has a Sub::Exporter to return functions wrapping a default instance
1117
1118 my $util = Crypt::Util->new;
1119
1120 $util->default_key("my secret");
1121
1122 # MAC or cipher+digest based tamper resistent encapsulation
1123 # (uses Storable on $data if necessary)
1124 my $tamper_resistent_string = $util->tamper_proof( $data );
1125
1126 my $verified = $util->thaw_tamper_proof( $untrusted_string, key => "another secret" );
1127
1128 # If the encoding is unspecified, base32 is used
1129 # (hex if base32 is unavailable)
1130 my $encoded = $util->encode_string( $bytes );
1131
1132 my $hash = $util->digest( $bytes, digest => "md5" );
1133
1134 die "baaaad" unless $util->verify_hash(
1135 hash => $hash,
1136 data => $bytes,
1137 digest => "md5",
1138 );
1139
1140
1141 =head1 DESCRIPTION
1142
1143 This module provides an easy, intuitive and forgiving API for wielding
1144 crypto-fu.
1145
1146 The API is designed as a cascade, with rich features built using simpler ones.
1147 this means that the option processing is uniform throughout, and the behaviors
1148 are generally predictable.
1149
1150 Note that L<Crypt::Util> doesn't do any crypto on its own, but delegates the
1151 actual work to the various other crypto modules on the CPAN. L<Crypt::Util>
1152 merely wraps these modules, providing uniform parameters, and building on top
1153 of their polymorphism with higher level features.
1154
1155 =head2 Priorities
1156
1157 =over 4
1158
1159 =item Ease of use
1160
1161 This module is designed to have an easy API to allow easy but responsible
1162 use of the more low level Crypt:: and Digest:: modules on CPAN. Therefore,
1163 patches to improve ease-of-use are very welcome.
1164
1165 =item Pluggability
1166
1167 Dependency hell is avoided using a fallback mechanism that tries to choose an
1168 algorithm based on an overridable list.
1169
1170 For "simple" use install Crypt::Util and your favourite digest, cipher and
1171 cipher mode (CBC, CFB, etc).
1172
1173 To ensure predictable behavior the fallback behavior can be disabled as necessary.
1174
1175 =back
1176
1177 =head2 Interoperability
1178
1179 To ensure that your hashes and strings are compatible with L<Crypt::Util>
1180 deployments on other machines (where different Crypt/Digest modules are
1181 available, etc) you should use C<disable_fallback>.
1182
1183 Then either set the default ciphers, or always explicitly state the cipher.
1184
1185 If you are only encrypting and decrypting with the same installation, and new
1186 cryptographic modules are not being installed, the hashes/ciphertexts should be
1187 compatible without disabling fallback.
1188
1189 =head1 EXPORTED API
1190
1191 B<NOTE>: nothing is exported by default.
1192
1193 L<Crypt::Util> also presents an optional exported api using L<Sub::Exporter>.
1194
1195 Unlike typical exported APIs, there is no class level default instance shared
1196 by all the importers, but instead every importer gets its own instance.
1197
1198 For example:
1199
1200 package A;
1201 use Crypt::Util qw/:all/;
1202
1203 default_key("moose");
1204 my $ciphertext = encrypt_string($plain);
1205
1206
1207 package B;
1208 use Crypt::Util qw/:all/;
1209
1210 default_key("elk");
1211 my $ciphertext = encrypt_string($plain);
1212
1213 In this example every importing package has its own implicit instance, and the
1214 C<default_key> function will in fact not share the value.
1215
1216 You can get the instance using the C<exported_instance> function, which is just
1217 the identity method.
1218
1219 The export tags supported are: C<crypt> (encryption and tamper proofing related
1220 functions), C<digest> (digest and MAC related functions), C<encoding> (various
1221 encoding and decoding functions), and C<params> which give you functions for
1222 handling default values.
1223
1224 =head1 METHODS
1225
1226 =over 4
1227
1228 =item tamper_proof( [ $data ], %params )
1229
1230 =item thaw_tamper_proof( [ $string ], %params )
1231
1232 =item tamper_proof_string( $string, %params )
1233
1234 =item thaw_tamper_proof_string( $string, %params )
1235
1236 =item aead_tamper_proof_string( [ $string ], %params )
1237
1238 =item mac_tamper_proof_string( [ $string ], %params )
1239
1240 The C<tamper_proof> method is in an intermittent state, in that the C<data>
1241 parameter's API is not completely finalized.
1242
1243 It is safer to use C<tamper_proof_string>; its API is expected to remain the
1244 same in future versions as well.
1245
1246 See L</TODO> for more information about the data types that will be supported
1247 in the future.
1248
1249 When thawing, the C<authenticated_decrypt_string> or C<verify_mac> methods will
1250 be used, with C<fatal> defaulting to on unless explicitly disabled in the
1251 parameters.
1252
1253 =over 4
1254
1255 This method accepts the following parameters:
1256
1257 =item * encrypt
1258
1259 By default this parameter is true, unless C<default_tamper_proof_unencrypted()>,
1260 has been enabled.
1261
1262 A true value implies that all the parameters
1263 which are available to C<authenticated_encrypt_string()> are also available.
1264 If a negative value is specified, MAC mode is used, and the additional
1265 parameters of C<mac_digest_string()> may also be specified to this method.
1266
1267 =item * data
1268
1269 The data to encrypt. If this is a reference L<Storable> will be used to
1270 serialize the data.
1271
1272 See C<pack_data> for details.
1273
1274 =back
1275
1276 If the string is encrypted then all the parameters of C<encrypt_string> and
1277 C<digest_string> are also available.
1278
1279 If the string is not encrypted, then all the parameters of C<mac_digest_string>
1280 are also available.
1281
1282 =item encrypt_string( [ $string ], %params )
1283
1284 =item decrypt_string( [ $string ], %params )
1285
1286 =item authenticated_encrypt_string( [ $string ], %params )
1287
1288 =item authenticated_decrypt_string( [ $string ], %params )
1289
1290 All of the parameters which may be supplied to C<process_key()>,
1291 C<cipher_object> and C<maybe_encode> are also available to these methods.
1292
1293 The C<authenticated> variants ensure that an authenticated encryption mode
1294 (such as EAX) is used.
1295
1296 The following parameters may be used:
1297
1298 =over 4
1299
1300 =item * string
1301
1302 The string to be en/decrypted can either be supplied first, creating an odd
1303 number of arguments, or as a named parameter.
1304
1305 =item * nonce
1306
1307 The cryptographic nonce to use. Only necessary for encryption, will be packed
1308 in the string as part of the message if applicable.
1309
1310 =item * header
1311
1312 Not yet supported.
1313
1314 In the future this will include a header for AEAD (the "associated data" bit of
1315 AEAD).
1316
1317 =back
1318
1319 =item process_key( [ $key ], %params )
1320
1321 The following arguments may be specified:
1322
1323 =over 4
1324
1325 =item * literal_key
1326
1327 This disables mungung. See also C<default_use_literal_key>.
1328
1329 =item * key_size
1330
1331 Can be used to force a key size, even if the cipher specifies another size.
1332
1333 If not specified, the key size chosen will depend
1334
1335 =item * cipher
1336
1337 Used to determine the key size.
1338
1339 =back
1340
1341 =item process_nonce( [ $nonce ], %params )
1342
1343 If a nonce is explicitly specified this method returns that, and otherwise uses
1344 L<Data::GUID> to generate a unique binary string for use as a nonce/IV.
1345
1346 =item pack_data( [ $data ], %params )
1347
1348 =item unpack_data( [ $data ], %params )
1349
1350 Uses L<Storable> and C<pack> to create a string out of data.
1351
1352 L<MooseX::Storage> support will be added in the future.
1353
1354 The format itself is versioned in order to facilitate future proofing and
1355 backwards compatibility.
1356
1357 Note that it is not safe to call C<unpack_data> on an untrusted string, use
1358 C<thaw_tamper_proof> instead (it will authenticate the data and only then
1359 perform the potentially unsafe routines).
1360
1361 =item cipher_object( %params )
1362
1363 Available parameters are:
1364
1365 =over 4
1366
1367 =item * cipher
1368
1369 The cipher algorithm to use, e.g. C<Rijndael>, C<Twofish> etc.
1370
1371 =item * mode
1372
1373 The mode of operation. This can be real (C<cbc>, C<cfb>, C<ctr>, C<ofb>,
1374 C<eax>) or symbolic (C<authenticated>, C<block>, C<stream>).
1375
1376 See L<http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation> for an
1377 explanation of this.
1378
1379 =back
1380
1381 =item cipher_object_eax( %params )
1382
1383 Used by C<cipher_object> but accepts additional parameters:
1384
1385 =over 4
1386
1387 =item * nonce
1388
1389 The nonce is a value that should be unique in order to protect against replay
1390 attacks. It also ensures that the same plain text with the same key will
1391 produce different ciphertexts.
1392
1393 The nonce is not included in the output ciphertext. See
1394 C<authenticated_encrypt_string> for a convenience method that does include the
1395 nonce.
1396
1397 =item * header
1398
1399 This is additional data to authenticate but not encrypt.
1400
1401 See L<Crypt::EAX> for more details.
1402
1403 The header will not be included in the output ciphertext.
1404
1405 =back
1406
1407 =item digest_string( [ $string ], %params )
1408
1409 Delegates to C<digest_object>. All parameters which can be used by
1410 C<digest_object> may also be used here.
1411
1412 The following arguments are available:
1413
1414 =over 4
1415
1416 =item * string
1417
1418 The string to be digested can either be supplied first, creating an odd
1419 number of arguments, or as a named parameter.
1420
1421 =back
1422
1423 =item verify_digest( %params )
1424
1425 Delegates to C<digest_object>. All parameters which can be used by
1426 C<digest_object> may also be used here.
1427
1428 The following parameters are accepted:
1429
1430 =over 4
1431
1432 =item * hash
1433
1434 A string containing the hash to verify.
1435
1436 =item * string
1437
1438 The digested string.
1439
1440 =item * fatal
1441
1442 If true, errors will be fatal. The default is false, which means that
1443 failures will return undef.
1444
1445 =back
1446
1447 In addition, the parameters which can be supplied to C<digest_string()>
1448 may also be supplied to this method.
1449
1450 =item digest_object( %params )
1451
1452 =over 4
1453
1454 =item * digest
1455
1456 The digest algorithm to use.
1457
1458 =back
1459
1460 Returns an object using L<Digest>.
1461
1462 =item encode_string( [ $string ], %params )
1463
1464 =item decode_string( [ $string ], %params )
1465
1466 The following parameters are accepted:
1467
1468 =over 4
1469
1470 =item * encoding
1471
1472 The encoding may be a symbolic type (uri, printable) or a concrete type
1473 (none, hex, base64, base32).
1474
1475 =back
1476
1477 =item mac_digest_string( [ $string ], %param )
1478
1479 Delegates to C<mac_object>. All parameters which can be used by C<mac_object>
1480 may also be used here.
1481
1482 =over 4
1483
1484 =item * string
1485
1486 =back
1487
1488 =item verify_mac( %params )
1489
1490 Delegates to C<mac_object>. All parameters which can be used by C<mac_object>
1491 may also be used here.
1492
1493 The following additional arguments are allowed:
1494
1495 =over 4
1496
1497 =item * hash
1498
1499 The MAC string to verify.
1500
1501 =item * string
1502
1503 The digested string.
1504
1505 =item * fatal
1506
1507 If true, errors will be fatal. The default is false, which means that
1508 failures will return undef.
1509
1510 =back
1511
1512 =item mac_object( %params )
1513
1514 =over 4
1515
1516 =item * mac
1517
1518 The MAC algorithm to use. Currently C<hmac> and C<cmac> are supported.
1519
1520 =back
1521
1522 =item maybe_encode
1523
1524 =item maybe_decode
1525
1526 This method has no external API but is documented for the sake of its shared
1527 options.
1528
1529 It is delegated to by the various encryption and digest method.
1530
1531 =over 4
1532
1533 =item * encode
1534
1535 Expects a bool.
1536
1537 =item * encoding
1538
1539 Expects an algorithm name (symbolic (e.g. C<uri>, C<alphanumeric>), or concrete
1540 (e.g. C<base64>, C<hex>)).
1541
1542 =back
1543
1544 If C<encode> is explicitly supplied it will always determine whether or not the
1545 string will be encoded. Otherwise, if C<encoding> is explicitly supplied then
1546 the string will always be encoded using the specified algorithm. If neither is
1547 supplied C<default_encode> will be checked to determine whether or not to
1548 encode, and C<default_encoding> or C<fallback_encoding> will be used to
1549 determine the algorithm to use (see L</HANDLING OF DEFAULT VALUES>).
1550
1551 =item encode_string_alphanumerical( $string )
1552
1553 =item decode_string_alphanumerical( $string )
1554
1555 =item encode_string_uri( $string )
1556
1557 =item decode_string_uri( $string )
1558
1559 =item encode_string_printable( $string )
1560
1561 =item decode_string_printable( $string )
1562
1563 The above methods encode based on a fallback list (see L</HANDLING OF DEFAULT VALUES>).
1564
1565 The variations denote types of formats: C<alphanumerical> is letters and
1566 numbers only (case insensitive), C<uri> is safe for inclusions in URIs (without
1567 further escaping), and C<printable> contains no control characters or
1568 whitespace.
1569
1570 =item encode_string_hex( $string )
1571
1572 =item decode_string_hex( $string )
1573
1574 Big endian hexadecimal (C<H*> pack format).
1575
1576 =item encode_string_uri_escape( $string )
1577
1578 =item decode_string_uri_escape( $string )
1579
1580 L<URI::Escape> based encoding.
1581
1582 =item encode_string_base64( $string )
1583
1584 =item decode_string_base64( $string )
1585
1586 =item encode_string_base64_wrapped( $string )
1587
1588 Requires L<MIME::Base64>.
1589
1590 The C<wrapped> variant will introduce line breaks as per the L<MIME::Base64>
1591 default>.
1592
1593 =item encode_string_uri_base64
1594
1595 =item decode_string_uri_base64
1596
1597 Requires L<MIME::Base64>.
1598
1599 Implements the Base64 for URIs. See
1600 L<http://en.wikipedia.org/wiki/Base64#URL_Applications>.
1601
1602 =item encode_string_base32( $string )
1603
1604 =item decode_string_base32( $string )
1605
1606 Requires L<MIME::Base32>.
1607
1608 (note- unlike L<MIME::Base32> this is case insensitive).
1609
1610 =head1 HANDLING OF DEFAULT VALUES
1611
1612 =over 4
1613
1614 =item disable_fallback()
1615
1616 When true only the first item from the fallback list will be tried, and if it
1617 can't be loaded there will be a fatal error.
1618
1619 Enable this to ensure portability.
1620
1621 =back
1622
1623 For every parameter, there are several methods, where PARAMETER is replaced
1624 with the parameter name:
1625
1626 =over 4
1627
1628 =item * default_PARAMETER()
1629
1630 This accessor is available for the user to override the default value.
1631
1632 If set to undef, then C<fallback_PARAMETER> will be consulted instead.
1633
1634 B<ALL> the default values are set to undef unless changed by the user.
1635
1636 =item * fallback_PARAMETER()
1637
1638 Iterates the C<fallback_PARAMETER_list>, choosing the first value that is
1639 usable (it's provider is available).
1640
1641 If C<disable_fallback> is set to a true value, then only the first value in the
1642 fallback list will be tried.
1643
1644 =item * fallback_PARAMETER_list()
1645
1646 An ordered list of values to try and use as fallbacks.
1647
1648 C<fallback_PARAMETER> iterates this list and chooses the first one that works.
1649
1650 =back
1651
1652 Available parameters are as follows:
1653
1654 =over 4
1655
1656 =item * cipher
1657
1658 The fallback list is
1659 C<Rijndael>, C<Serpent>, C<Twofish>, C<RC6>, C<Blowfish> and C<RC5>.
1660
1661 L<Crypt::Rijndael> is the AES winner, the next three are AES finalists, and the
1662 last two are well known and widely used.
1663
1664 =item * mode
1665
1666 The mode in which to use the cipher.
1667
1668 The fallback list is C<CFB>, C<CBC>, C<Ctr>, and C<OFB>.
1669
1670 =item digest
1671
1672 The fallback list is C<SHA-1>, C<SHA-256>, C<RIPEMD160>,
1673 C<Whirlpool>, C<MD5>, and C<Haval256>.
1674
1675 =item * encoding
1676
1677 The fallback list is C<hex> (effectively no fallback).
1678
1679 =item alphanumerical_encoding
1680
1681 The fallback list is C<base32> and C<hex>.
1682
1683 L<MIME::Base32> is required for C<base32> encoding.
1684
1685 =item * uri_encoding
1686
1687 The fallback list is C<uri_base64>.
1688
1689 =item * printable_encoding
1690
1691 The fallback list is C<base64>
1692
1693 =back
1694
1695 =head2 Defaults with no fallbacks
1696
1697 The following parameters have a C<default_> method, as described in the
1698 previous section, but the C<fallback_> methods are not applicable.
1699
1700 =over 4
1701
1702 =item * encode
1703
1704 Whether or not to encode by default (applies to digests and encryptions).
1705
1706 =item * key
1707
1708 The key to use. Useful for when you are repeatedly encrypting.
1709
1710 =item * nonce
1711
1712 The nonce/IV to use for cipher modes that require it.
1713
1714 Defaults to the empty string, but note that some methods will generate a nonce
1715 for you (e.g. C<authenticated_encrypt_string>) if none was provided.
1716
1717 =item * use_literal_key
1718
1719 Whether or not to not hash the key by default. See C<process_key>.
1720
1721 =item * tamper_proof_unencrypted
1722
1723 Whether or not tamper resistent strings are by default unencrypted (just MAC).
1724
1725 =back
1726
1727 =head2 Subclassing
1728
1729 You may safely subclass and override C<default_PARAMETER> and
1730 C<fallback_PARAMETER_list> to provide values from configurations.
1731
1732 =back
1733
1734 =head1 TODO
1735
1736 =over 4
1737
1738 =item *
1739
1740 Crypt::SaltedHash support
1741
1742 =item *
1743
1744 EMAC (maybe, the modules are not OO and require refactoring) message
1745 authentication mode
1746
1747 =item *
1748
1749 Bruce Schneier Fact Database
1750 L<http://geekz.co.uk/lovesraymond/archive/bruce-schneier-facts>.
1751
1752 L<WWW::SchneierFacts>
1753
1754 =item *
1755
1756 Entropy fetching (get N weak/strong bytes, etc) from e.g. OpenSSL bindings,
1757 /dev/*random, and EGD.
1758
1759 =item *
1760
1761 Additional data formats (streams/iterators, filehandles, generalized storable
1762 data/string handling for all methods, not just tamper_proof).
1763
1764 Streams should also be able to used via a simple push api.
1765
1766 =item *
1767
1768 IV/nonce/salt support for the various cipher modes, not just EAX (CBC, CCM, GCM, etc)
1769
1770 =item *
1771
1772 L<Crypt::Rijndael> can do its own cipher modes
1773
1774 =head1 SEE ALSO
1775
1776 L<Digest>, L<Crypt::CBC>, L<Crypt::CFB>,
1777 L<http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation>.
1778
1779 =head1 VERSION CONTROL
1780
1781 This module is maintained using Darcs. You can get the latest version from
1782 L<http://nothingmuch.woobling.org/Crypt-Util/>, and use C<darcs send> to commit
1783 changes.
1784
1785 =head1 AUTHORS
1786
1787 Yuval Kogman, E<lt>[email protected]<gt>
1788 Ann Barcomb
1789
1790 =head1 COPYRIGHT & LICENSE
1791
1792 Copyright 2006-2008 by Yuval Kogman E<lt>[email protected]<gt>, Ann Barcomb
1793
1794 Permission is hereby granted, free of charge, to any person obtaining a copy
1795 of this software and associated documentation files (the "Software"), to deal
1796 in the Software without restriction, including without limitation the rights
1797 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1798 copies of the Software, and to permit persons to whom the Software is
1799 furnished to do so, subject to the following conditions:
1800
1801 The above copyright notice and this permission notice shall be included in
1802 all copies or substantial portions of the Software.
1803
1804 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1805 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1806 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1807 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1808 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1809 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1810 DEALINGS IN THE SOFTWARE.
1811
1812 =cut