Debian Codegraph @

t/tamper.t

raw symbols SCIP
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8
9 use Crypt::Util;
10
11 my $c;
12
13 BEGIN {
14 $c = Crypt::Util->new;
15
16 eval { $c->fallback_digest; $c->fallback_cipher; $c->fallback_mac; $c->fallback_authenticated_mode };
17 plan skip_all => "$1" if $@ =~ /(Couldn't load any \w+)/;
18 plan skip_all => "Couldn't load fallback" if $@;
19
20 plan 'no_plan';
21 }
22
23 $c->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_ok { $tamper = $c->tamper_proof( data => $data, encrypt => $encrypted ) } "tamper proofing lived (" . ($encrypted ? "aead" : "mac signed") .")";
36
37 ok( defined($tamper), "got some output" );
38
39 unless ( ref $data ) {
40 if ( $encrypted ) {
41 unlike( $tamper, qr/\Q$data/, "tamper proof does not contain the original" )
42 } else {
43 like( $tamper, qr/\Q$data/, "tamper proof contains the original" )
44 }
45 }
46
47 my $thawed;
48
49 lives_ok { $thawed = $c->thaw_tamper_proof( string => $tamper ) } "tamper proof thaw lived";
50
51 ok( defined($thawed), "got some output" );
52
53 is_deeply( $thawed, $data, "tamper resistence round trips (" . ($encrypted ? "aead" : "mac signed") .")" );
54
55 my $corrupt_tamper = $tamper;
56 substr( $corrupt_tamper, -10, 5 ) ^= "moose";
57
58 throws_ok {
59 $c->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_proof($tamper);
66 $twaddled_tamper = $c->decrypt_string( string => $inner );
67 substr( $twaddled_tamper, -10, 5 ) ^= "moose";
68 $twaddled_tamper = $c->_pack_tamper_proof($type, $c->encrypt_string( string => $twaddled_tamper ));
69 } else {
70 $twaddled_tamper = $tamper;
71 substr( $twaddled_tamper, -10, 5 ) ^= "moose";
72 }
73
74 throws_ok {
75 $c->thaw_tamper_proof( string => $twaddled_tamper );
76 } qr/verification.*failed/i, "altered tamper proof string failed";
77
78 local $Crypt::Util::PACK_FORMAT_VERSION = 2;
79
80 throws_ok {
81 $c->thaw_tamper_proof( string => $tamper );
82 } qr/Incompatible packed string/, "version check";
83 }
84
85 }