1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
BEGIN { push @INC, qw(. .. ../lib ../../lib ../../../lib) }
package Crypt::Blowfish;
require Exporter;
require DynaLoader;
@ISA = (Exporter, DynaLoader);
bootstrap Crypt::Blowfish;
use Benchmark qw(timediff timestr);
use strict;
use Carp;
sub usage
{
my ($package, $filename, $line, $subr) = caller(1);
$Carp::CarpLevel = 2;
croak "Usage: $subr(@_)";
}
sub blocksize { 8; }
sub keysize { 56; }
sub new
{
usage("new Blowfish key") unless @_ == 2;
my $type = shift; my $self = {}; bless $self, $type;
$self->{'ks'} = Crypt::Blowfish::init(shift);
$self;
}
sub encrypt
{
usage("encrypt data[8 bytes]") unless @_ == 2;
my $self = shift;
my $data = shift;
Crypt::Blowfish::crypt($data, $data, $self->{'ks'}, 0);
$data;
}
sub decrypt
{
usage("decrypt data[8 bytes]") unless @_ == 2;
my $self = shift;
my $data = shift;
Crypt::Blowfish::crypt($data, $data, $self->{'ks'}, 1);
$data;
}
package main;
#
# '6162636465666768696a6b6c6d6e6f707172737475767778797a',
# '424c4f5746495348', '324ed0fef413a203',
#
# '57686f206973204a6f686e2047616c743f', 'fedcba9876543210', 'cc91732b8022f684')
#
# "Ayn Rand" FEDCBA9876543210 e113f4102cfcce43
#
print "1..11\n";
my $key = pack("H*", "6162636465666768696a6b6c6d6e6f707172737475767778797a");
my $in = pack("H*", "424c4f5746495348");
my $out = pack("H*", "324ed0fef413a203");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 1\n";
my $key = pack("H*", "57686f206973204a6f686e2047616c743f");
my $in = pack("H*", "fedcba9876543210");
my $out = pack("H*", "cc91732b8022f684");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->decrypt($out) eq $in);
print "ok 2\n";
my $key = "Ayn Rand";
substr($key, 3, 1) = pack("C", ord(substr($key, 3, 1))+128);
substr($key, 7, 1) = pack("C", ord(substr($key, 7, 1))+128);
my $in = pack("H*", "fedcba9876543210");
my $out = pack("H*", "e113f4102cfcce43");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 3\n";
my $key = pack("H*", "0000000000000000");
my $in = pack("H*", "0000000000000000");
my $out = pack("H*", "4EF997456198DD78");
my $cipher = new Crypt::Blowfish $key;
print "not " unless($cipher->encrypt($in) eq $out);
print "ok 4\n";
my $key = pack("H*", "FFFFFFFFFFFFFFFF");
my $in = pack("H*", "0000000000000000");
my $out = pack("H*", "F21E9A77B71C49BC");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->decrypt($out) eq $in);
print "ok 5\n";
my $key = pack("H*", "FFFFFFFFFFFFFFFF");
my $in = pack("H*", "FFFFFFFFFFFFFFFF");
my $out = pack("H*", "51866FD5B85ECB8A");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 6\n";
my $key = pack("H*", "3000000000000000");
my $in = pack("H*", "1000000000000001");
my $out = pack("H*", "7D856F9A613063F2");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 7\n";
my $key = pack("H*", "1111111111111111");
my $in = pack("H*", "1111111111111111");
my $out = pack("H*", "2466DD878B963C9D");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 8\n";
my $key = pack("H*", "584023641ABA6176");
my $in = pack("H*", "004BD6EF09176062");
my $out = pack("H*", "452031C1E4FADA8E");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 9\n";
my $key = pack("H*", "0101010101010101");
my $in = pack("H*", "0123456789ABCDEF");
my $out = pack("H*", "FA34EC4847B268B2");
my $cipher = new Crypt::Blowfish $key;
print "not " unless ($cipher->encrypt($in) eq $out);
print "ok 10\n";
{
print "\nRunning standard mode speed tests.. encrypting with non-cached cipher\n";
my $t0 = new Benchmark;
for(1..5000){
my $key = pack("H*", "0101010101010101");
my $in = pack("H*", "0123456789ABCDEF");
my $out = pack("H*", "FA34EC4847B268B2");
my $cipher = new Crypt::Blowfish $key;
$cipher->encrypt($in);
}
my $t1 = new Benchmark;
my $td = Benchmark::timediff($t1,$t0);
my $ts = Benchmark::timestr($td);
print "5,000 cycles: $ts\n";
}; # end standard mode (encrypt) speed test non-cached cipher
{
print "\nRunning standard mode speed tests.. decrypting with non-cached cipher\n";
my $t0 = new Benchmark;
for(1..5000){
my $key = pack("H*", "0101010101010101");
my $in = pack("H*", "0123456789ABCDEF");
my $out = pack("H*", "FA34EC4847B268B2");
my $cipher = new Crypt::Blowfish $key;
$cipher->decrypt($out);
}
my $t1 = new Benchmark;
my $td = Benchmark::timediff($t1,$t0);
my $ts = Benchmark::timestr($td);
print "5,000 cycles: $ts\n";
}; # end standard mode (decrypt) speed test non-cached cipher
my $key = pack("H*", "0101010101010101");
my $cipher = new Crypt::Blowfish $key;
{
print "\nRunning standard mode speed tests.. encrypting with cached cipher\n";
my $t0 = new Benchmark;
for(1..10000){
my $in = pack("H*", "0123456789ABCDEF");
my $out = pack("H*", "FA34EC4847B268B2");
$cipher->encrypt($in);
}
my $t1 = new Benchmark;
my $td = Benchmark::timediff($t1,$t0);
my $ts = Benchmark::timestr($td);
print "10,000 cycles: $ts\n";
}; # end standard mode (encrypt) speed test cached cipher
{
print "\nRunning standard mode speed tests.. decrypting with cached cipher\n";
my $t0 = new Benchmark;
for(1..10000){
my $in = pack("H*", "0123456789ABCDEF");
my $out = pack("H*", "FA34EC4847B268B2");
$cipher->decrypt($out);
}
my $t1 = new Benchmark;
my $td = Benchmark::timediff($t1,$t0);
my $ts = Benchmark::timestr($td);
print "10,000 cycles: $ts\n";
}; # end standard mode (decrypt) speed test cached cipher
print "\nTesting Cipher Block Chaining..\n";
eval 'use Crypt::CBC';
if(!$@) {
if($Crypt::CBC::VERSION < 1.22) {
$@ = "CBC mode requires Crypt::CBC version 1.22 or higher.";
} else {
my $cipher = new Crypt::CBC(pack("H*","0123456789ABCDEFF0E1D2C3B4A59687"),"Blowfish");
my $ciphertext = $cipher->encrypt(pack("H*","37363534333231204E6F77206973207468652074696D6520666F722000"));
my $plaintext = $cipher->decrypt($ciphertext);
if($plaintext eq "7654321 Now is the time for \0") {
print "ok 11 - CBC Mode\n";
} else {
print unpack("H*",$plaintext) . " :decrypted\n";
print unpack("H*","7654321 Now is the time for \0") . " :orig\n";
print "not ok 11 - CBC Mode failed\n";
}
}
} # end no errors
if($@) {
print "Error (probably harmless):\n$@\n";
}
|