| 1 | #!/usr/bin/perl -w | ||
| 2 | # $Id: comp.pl,v 1.6 2001/08/12 20:05:02 lackas Exp $ | ||
| 3 | |||
| 4 | # Compare results from Crypt::Rijndael_PP with Crypt::Rijndael | ||
| 5 | # Press Ctrl-C to stop the test (on Unix) | ||
| 6 | # prints '.' for every correct calculation and dies | ||
| 7 | # if the results are different | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use blib qw'./blib'; | ||
| 11 | use Crypt::Rijndael_PP; | ||
| 12 | use Crypt::Rijndael; | ||
| 13 | |||
| 14 | my ($count,$bytes) = 0; | ||
| 15 | |||
| 16 | my $mult = 10; | ||
| 17 | |||
| 18 | sub result { print "\n$count rounds\n$bytes Bytes\n"; exit; }; | ||
| 19 | |||
| 20 | $SIG{INT} = \&result; | ||
| 21 | |||
| 22 | my $mode = shift || 'CBC'; | ||
| 23 | my $key_size = 256; | ||
| 24 | my $plain_size = 128; | ||
| 25 | |||
| 26 | my %MODE = ( | ||
| 27 | ECB => { | ||
| 28 | c_xs => Crypt::Rijndael::MODE_ECB, | ||
| 29 | c_pl => Crypt::Rijndael_PP::MODE_ECB | ||
| 30 | }, | ||
| 31 | CBC => { | ||
| 32 | c_xs => Crypt::Rijndael::MODE_CBC, | ||
| 33 | c_pl => Crypt::Rijndael_PP::MODE_CBC | ||
| 34 | } | ||
| 35 | ); | ||
| 36 | |||
| 37 | $|++; | ||
| 38 | while(1) { | ||
| 39 | my $key = gen($key_size); | ||
| 40 | my $c_xs = Crypt::Rijndael->new($key, $MODE{$mode}{c_xs}); | ||
| 41 | my $c_pl = Crypt::Rijndael_PP->new($key, $MODE{$mode}{c_pl}); | ||
| 42 | |||
| 43 | for (1..10) { | ||
| 44 | my $plain = gen($plain_size * (int(rand 16) +1 )); | ||
| 45 | my $t_xs = $c_xs->encrypt($plain); | ||
| 46 | my $t_pl = $c_pl->encrypt($plain); | ||
| 47 | my $p_pl = $c_pl->decrypt($t_pl); | ||
| 48 | my $p_xs = $c_xs->decrypt($t_pl); | ||
| 49 | if ($t_xs ne $t_pl) { | ||
| 50 | print "Ciphertext differs.\n"; | ||
| 51 | print "Input: ", unpack('H*',$plain),"\n"; | ||
| 52 | print "XS : ", unpack('H*',$t_xs),"\n"; | ||
| 53 | print "PL : ", unpack('H*',$t_pl),"\n"; | ||
| 54 | exit; | ||
| 55 | } elsif ($p_pl ne $plain) { | ||
| 56 | print "Plaintext differs.\n"; | ||
| 57 | print "Input: ", unpack('H*',$plain),"\n"; | ||
| 58 | print "PL : ", unpack('H*',$t_pl),"\n"; | ||
| 59 | print "Out : ", unpack('H*',$p_pl),"\n"; | ||
| 60 | exit; | ||
| 61 | } elsif ($p_xs ne $plain) { | ||
| 62 | print "XS decode failed.\n"; | ||
| 63 | print "Input: ", unpack('H*',$plain),"\n"; | ||
| 64 | print "PL : ", unpack('H*',$t_pl),"\n"; | ||
| 65 | print "o-pl : ", unpack('H*',$p_pl),"\n"; | ||
| 66 | print "o-xs : ", unpack('H*',$p_xs),"\n"; | ||
| 67 | } else { | ||
| 68 | print '.' | ||
| 69 | } | ||
| 70 | $count++; $bytes+=length $plain; | ||
| 71 | } | ||
| 72 | print ':'; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub gen { | ||
| 76 | my $size = shift; | ||
| 77 | my $res; | ||
| 78 | while ($size > 0) { | ||
| 79 | $size -= 8; | ||
| 80 | $res .= pack 'C', rand 256; | ||
| 81 | } | ||
| 82 | $res; | ||
| 83 | } | ||
| 84 |