#! /usr/bin/perl -w
############################################################
## Copyright: 2011 Integrated Sytems Laboratory, ETH Zurich
## http://www.iis.ee.ethz.ch/~sha3
############################################################
use strict;
# v0.2 - kgf@ee.ethz.ch - Fri Aug 12 14:10:07 CEST 2011
# - added the modulo for blake and sha2
# v0.2 - kgf@ee.ethz.ch - Thu Aug 11 08:38:45 CEST 2011
# - Length parameter changed to bits
# - Introduced the algorithm specific modulo to determine
# the bit size
# - Starts a localized script to generate the stimuli files
# v0.1 - kgf@ee.ethz.ch - Tue Jul 26 10:10:49 CEST 2011
# - This should generate the stimuli files for the SHA3 algorithms
# ToDo:
# - Have a database of reference values (KAT)
# - The length is limited to the (algorithm specific) modulo values
# as determined by the $modulo{$alg}. This limits the possinble
# input values, and is there purely for convenience reasons.
## CONSTANTS
my $MAX_RANDOM_NUMBITS = 16384;
my $MAX_RANDOM_VECTORS = 1000;
my $STIMULI_FILE = "testvectors.txt";
## get the number of vectors to be generated
my $num_vectors = ($#ARGV>-1)? shift: 10;
## perhaps we want it randomized ??
$num_vectors = ($num_vectors=~/ra?ndo?m?/i)? int(rand($MAX_RANDOM_VECTORS)) : $num_vectors;
## second argument is how many bits we want
my $num_bits= ($#ARGV>-1)? shift: 512;
## get the name of the algorithm from the pwd
my $pwd =`pwd`;
unless ($pwd=~/\/(ethz|gmu)_(blake|groestl|jh|keccak|skein|sha2)\/simvectors/){
die "\nI am not sure the path is correct:\n\t${pwd}exiting\n";
}
my $alg=$2;
my $flavor=$1;
my %names= ( "blake" => "Jean-Philippe Aumasson",
"groestl" => "Groestl Team",
"jh" => "Hongjun Wu",
"keccak" => "Keccak Team",
"sha2" => "NIST",
"skein" => "Bruce Schneier");
## tells what is the modulo in which the vectors have to be generated
## make sure this is at least 8 for the moment (random number generating
## part accepts number of bytes. Should not be a major problem
my %modulo= ( "blake" => 16,
"groestl" => 16,
"jh" => 16,
"keccak" => 16,
"sha2" => 8,
"skein" => 16);
print "Generating $num_vectors vectors of length ${num_bits} bits for\n";
print "Algorithm $alg (by $names{$alg}) - $flavor flavor\n";
open (S, "> stimuli.txt") or die "Can not create stimuli.txt, exiting\n";
print S <<"BUGU";
# stimuli.txt
# Algorithm Name: $alg
# Principal Submitter: $names{$alg}
BUGU
for my $i (1..$num_vectors){
## perhaps we want it randomized ??
my $num_act_bits= ($num_bits=~/ra?ndo?m?/i)? int(rand($MAX_RANDOM_NUMBITS)/$modulo{$alg})*$modulo{$alg}+$modulo{$alg}
: int($num_bits/$modulo{$alg})*$modulo{$alg};
## this case is not handled correctly.. ethz blake makes mistakes for block lengths between
## 448 - 512 (modulo 512) inclusive
if ( ($flavor eq "ethz") and ($alg eq "blake") and
( ($num_act_bits % 512 == 0) or (($num_act_bits % 512) ge 448) and (($num_act_bits % 512) le 511))
) { $num_act_bits = 128 }
my $num_bytes= int($num_act_bits / 8) ; ## make sure the modulo is at least 8
my $data=uc(random_data($num_bytes));
print S <<"BUGU";
Len = $num_act_bits
Msg = $data
MD = ??
BUGU
}
close(S);
# now we are finished
# run ETHZ_KAT so that the expected responses are there
if (-e "ETHZ_KAT"){
print '-' x 60 ."\n";
print "running ETHZ_KAT to generate expresp.txt\n";
system ("./ETHZ_KAT");
print "done..\n";
print '-' x 60 ."\n";
}
else{
die "We need ETHZ_KAT executable for $alg compiled first\n";
}
## if one exists, start the post-processing step
my $script = "${flavor}_${alg}_stimuli.pl";
if (-e $script){
print "running $script to generate the $STIMULI_FILE\n";
system ("./$script > $STIMULI_FILE");
print "done..\n";
print '-' x 60 ."\n";
}
else{
die "Can not find algorithm specific post processing script [$script]\n";
}
sub random_data {
my $num_bits = ($#_ > -1) ? $_[0] : 16;
# will generate a 128 bit data, can be used as input vector or key
my $retval ="";
for (1..$num_bits) {
$retval = $retval . unpack ('H*', pack ('C1',int(rand(256))));
}
return $retval;
}