#!perl
push(@INC,'/afs/athena.mit.edu/user/e/eichin/perl');
require 'des.perl';

print 'Examples per FIPS publication 81, keys ivs and cipher
in hex.  These are the correct answers, see below for
the actual answers.
';

print 'Examples per Davies and Price.
';

print 'EXAMPLE ECB     key = 08192a3b4c5d6e7f
        clear = 0
        cipher = 25 dd ac 3e 96 17 64 67
';

$key = pack("C8",0x08, 0x19, 0x2a, 0x3b, 0x4c, 0x5d, 0x6e, 0x7f);
$schedule="";
&make_key_sched($key,$schedule);
$datum = pack("x8",1);
($left,$right) = unpack("N N",$datum);
$temp = 0;
&DES_DO_ENCRYPT($left, $right, $temp, $schedule);
$outdat = pack("N N",$left,$right);

print 'ACTUAL ECB:
        cipher = ';
printf "%02x " x 8,unpack("C" x 8,$outdat);
print '

';


print 'EXAMPLE ECB     key = 0123456789abcdef
        clear = "Now is the time for all "
        cipher = 3f a4 0e 8a 98 4d 48 15 ...
';

$key = pack("C8",0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef);
$schedule="";
&make_key_sched($key,$schedule);
$datum = "Now is the time for all ";
($left,$right) = unpack("N N",$datum);
$temp = 0;
&DES_DO_ENCRYPT($left, $right, $temp, $schedule);
$outdat = pack("N N",$left,$right);

print 'ACTUAL ECB:
        cipher = ';
printf "%02x " x 8,unpack("C" x 8,$outdat);
print '

';



print 'EXAMPLE CBC     key = 0123456789abcdef  iv = 1234567890abcdef
        clear = "Now is the time for all "
        cipher =        e5 c7 cd de 87 2b f2 7c
                        43 e9 34 00 8c 38 9c 0f
                        68 37 88 49 9a 7c 05 f6
';

$key = pack("C8",0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef);
$ivec = pack("C8",0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef);
$schedule="";
&make_key_sched($key,$schedule);
$datum = "Now is the time for all ";
$outdat = "";
&des_cbc_encrypt($datum, $outdat, length($datum), $schedule, $ivec, 1);

print 'ACTUAL ECB:
        cipher =
';
$form = "%02x " x 8;
$form = "                        ".$form."\n";

printf $form x (length($outdat)/8),unpack("C" x length($outdat),$outdat);
print '

';




print 'EXAMPLE CBC checksum    key =  0123456789abcdef iv =  1234567890abcdef
        clear =         "7654321 Now is the time for "
        checksum        58 d2 e7 7e 86 06 27 33, or some part thereof
';


$key = pack("C8",0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef);
$ivec = pack("C8",0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef);
$schedule="";
&make_key_sched($key,$schedule);
$datum = "7654321 Now is the time for ";
$outdat = "";
&des_cbc_cksum($datum, $outdat, length($datum), $schedule, $ivec);

print 'ACTUAL checksum:
        cipher =
';
$form = "%02x " x 8;
$form = "                        ".$form."\n";

printf $form x (length($outdat)/8),unpack("C" x length($outdat),$outdat);
print '

';




