| 1 | #!/usr/bin/perl |
| 2 | |
| 3 | use strictscip-perl cpan Crypt-Util 0.11 strict/; |
| 4 | use warningsscip-perl cpan Crypt-Util 0.11 warnings/; |
| 5 | |
| 6 | use Test::Morescip-perl cpan Crypt-Util 0.11 Test/More/; |
| 7 | use Test::Exceptionscip-perl cpan Crypt-Util 0.11 Test/Exception/; |
| 8 | |
| 9 | use Crypt::Utilscip-perl cpan Crypt-Util 0.11 Crypt/Util/; |
| 10 | |
| 11 | my $c; |
| 12 | |
| 13 | BEGIN { |
| 14 | $c = Crypt::Util->newscip-perl cpan Crypt-Util 0.11 Crypt/Util/new().; |
| 15 | |
| 16 | eval { $c->fallback_digestscip-perl cpan Crypt-Util 0.11 Crypt/Util/fallback_digest().; $c->fallback_cipherscip-perl cpan Crypt-Util 0.11 Crypt/Util/fallback_cipher().; $c->fallback_macscip-perl cpan Crypt-Util 0.11 Crypt/Util/fallback_mac().; $c->fallback_authenticated_modescip-perl cpan Crypt-Util 0.11 Crypt/Util/fallback_authenticated_mode(). }; |
| 17 | planscip-perl cpan Crypt-Util 0.11 main/plan(). skip_all => "$1" if $@scip-perl cpan Crypt-Util 0.11 main/`$@`. =~ /(Couldn't load any \w+)/; |
| 18 | planscip-perl cpan Crypt-Util 0.11 main/plan(). skip_all => "Couldn't load fallback" if $@scip-perl cpan Crypt-Util 0.11 main/`$@`.; |
| 19 | |
| 20 | planscip-perl cpan Crypt-Util 0.11 main/plan(). 'no_plan'; |
| 21 | } |
| 22 | |
| 23 | $c->default_keyscip-perl cpan Crypt-Util 0.11 Crypt/Util/default_key().("foo"); |
| 24 | |
| 25 | foreach my $encrypted ( 1, 0 ) { # encrypted not yet supported |
| 26 | |
| 27 | foreach my $data ( |
| 28 | "zemoose gauhy tj lkj GAJE E djjjj laaaa di da dooo", |
| 29 | { foo => "bar", gorch => [ qw/very deep/, 1 .. 10 ] }, |
| 30 | "\0 bar evil binary string \0 \0\0 foo la \xff foo \0 bar", |
| 31 | ) { |
| 32 | |
| 33 | my $tamper; |
| 34 | |
| 35 | lives_okscip-perl cpan Crypt-Util 0.11 main/lives_ok(). { $tamper = $c->tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/tamper_proof().( data => $data, encrypt => $encrypted ) } "tamper proofing lived (" . ($encrypted ? "aead" : "mac signed") .")"; |
| 36 | |
| 37 | okscip-perl cpan Crypt-Util 0.11 main/ok().( definedscip-perl cpan Crypt-Util 0.11 main/defined().($tamper), "got some output" ); |
| 38 | |
| 39 | unless ( refscip-perl cpan Crypt-Util 0.11 main/ref(). $data ) { |
| 40 | if ( $encrypted ) { |
| 41 | unlikescip-perl cpan Crypt-Util 0.11 main/unlike().( $tamper, qr/\Q$data/, "tamper proof does not contain the original" ) |
| 42 | } else { |
| 43 | likescip-perl cpan Crypt-Util 0.11 main/like().( $tamper, qr/\Q$data/, "tamper proof contains the original" ) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | my $thawed; |
| 48 | |
| 49 | lives_okscip-perl cpan Crypt-Util 0.11 main/lives_ok(). { $thawed = $c->thaw_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/thaw_tamper_proof().( string => $tamper ) } "tamper proof thaw lived"; |
| 50 | |
| 51 | okscip-perl cpan Crypt-Util 0.11 main/ok().( definedscip-perl cpan Crypt-Util 0.11 main/defined().($thawed), "got some output" ); |
| 52 | |
| 53 | is_deeplyscip-perl cpan Crypt-Util 0.11 main/is_deeply().( $thawed, $data, "tamper resistence round trips (" . ($encrypted ? "aead" : "mac signed") .")" ); |
| 54 | |
| 55 | my $corrupt_tamper = $tamper; |
| 56 | substrscip-perl cpan Crypt-Util 0.11 main/substr().( $corrupt_tamper, -10, 5 ) ^= "moose"; |
| 57 | |
| 58 | throws_okscip-perl cpan Crypt-Util 0.11 main/throws_ok(). { |
| 59 | $c->thaw_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/thaw_tamper_proof().( string => $corrupt_tamper ); |
| 60 | } qr/verification.*failed/i, "corrupt tamper proof string failed"; |
| 61 | |
| 62 | |
| 63 | my $twaddled_tamper; |
| 64 | if ( $encrypted ) { |
| 65 | my ( $type, $inner ) = $c->_unpack_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/_unpack_tamper_proof().($tamper); |
| 66 | $twaddled_tamper = $c->decrypt_stringscip-perl cpan Crypt-Util 0.11 Crypt/Util/decrypt_string().( string => $inner ); |
| 67 | substrscip-perl cpan Crypt-Util 0.11 main/substr().( $twaddled_tamper, -10, 5 ) ^= "moose"; |
| 68 | $twaddled_tamper = $c->_pack_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/_pack_tamper_proof().($type, $c->encrypt_stringscip-perl cpan Crypt-Util 0.11 Crypt/Util/encrypt_string().( string => $twaddled_tamper )); |
| 69 | } else { |
| 70 | $twaddled_tamper = $tamper; |
| 71 | substrscip-perl cpan Crypt-Util 0.11 main/substr().( $twaddled_tamper, -10, 5 ) ^= "moose"; |
| 72 | } |
| 73 | |
| 74 | throws_okscip-perl cpan Crypt-Util 0.11 main/throws_ok(). { |
| 75 | $c->thaw_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/thaw_tamper_proof().( string => $twaddled_tamper ); |
| 76 | } qr/verification.*failed/i, "altered tamper proof string failed"; |
| 77 | |
| 78 | local $Crypt::Util::PACK_FORMAT_VERSIONscip-perl cpan Crypt-Util 0.11 Crypt/Util/$PACK_FORMAT_VERSION. = 2; |
| 79 | |
| 80 | throws_okscip-perl cpan Crypt-Util 0.11 main/throws_ok(). { |
| 81 | $c->thaw_tamper_proofscip-perl cpan Crypt-Util 0.11 Crypt/Util/thaw_tamper_proof().( string => $tamper ); |
| 82 | } qr/Incompatible packed string/, "version check"; |
| 83 | } |
| 84 | |
| 85 | } |