#! /usr/sepp/bin/perl -w
############################################################
## Copyright: 2011 Integrated Sytems Laboratory, ETH Zurich
## http://www.iis.ee.ethz.ch/~sha3
############################################################
use strict;
# v0.1 - muheim@ee.ethz.ch - Fri Aug 12 09:31:00 CEST 2011
# - script to generate the vectors from the gen_sha3_stimuli
# copy from ethz_keccak_stimuli.pl v0.3
# make sure the lengths are ok later on
## CONSTANTS
my $HASH_BLOCK_SIZE= 512; # Keccak SHA-256 version
my $MIN_PADDING_SIZE= 80; # this is not completely correct. Min size is
# 1 + 64 but it is so much easier to generate 16 + 64
## let us extract the information first
my @L; # length array
my @M; # message array
my @D; # digest array
# steal all the information
# Note: if one of the entries MD, Len, Msg is missing trouble will arise
open (S, "< expresp.txt");
while (S){
push (@L,$1) if (/^\s*Len\s*=\s*([0-9]+)/i);
push (@M,$1) if (/^\s*Msg\s*=\s*([0-9A-F]+)/i);
push (@D,$1) if (/^\s*MD\s*=\s*([0-9A-F]+)/i);
}
close(S);
my $date= `date`;
chomp ($date);
print <<"BUGU";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Test vectors for ETHZ BLAKE
%% generated by ethz_blake_stimuli.pl
%% on $date
%%
BUGU
## loop over all read messages
for my $i (0..$#L){
## how many blocks will we need
my $num_blocks=int (($L[$i] + $MIN_PADDING_SIZE - 1) / $HASH_BLOCK_SIZE) +1;
## now let us determine the padding
## It is very easy to calculate this provided the pad size is at least 16
## it is a restriction at the moment but it works easily..
## for other lengths we need a more elaborate code..
my $zero_cnt = (($HASH_BLOCK_SIZE * $num_blocks) - $L[$i] - $MIN_PADDING_SIZE) / 8;
my $pad = "80"."00" x $zero_cnt."01";
# my $len = unpack ("H16", pack("N2",$L[$i]));
my $len = sprintf("%016x\n", $L[$i]);
$pad=$pad.$len;
#print $L[$i]." ".$len." ".$num_blocks." ".$zero_cnt."\n";
#print "$zero_cnt = (($HASH_BLOCK_SIZE * $num_blocks) - $L[$i] - $MIN_PADDING_SIZE)\n";
## these two are so that the reports look cute
my $pretty_i = $i + 1;
my $pretty_len = $#L + 1;
# comment character is '%' n the testbench
print <<"BUGU";
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% vector $pretty_i of $pretty_len
%% Length = $L[$i], Number of $HASH_BLOCK_SIZE bit blocks = $num_blocks, Padding = $pad
%% Message = $M[$i]
%% Digest = $D[$i]
BUGU
## add the pad and the length to the message
$M[$i]=$M[$i].$pad;
## now loop over number of blocks
for my $j (1..$num_blocks){
## this is the part of the message that goes out
my $sub=substr($M[$i],($j-1)*($HASH_BLOCK_SIZE/4),($HASH_BLOCK_SIZE/4));
## generate the output
print unpack("B*",pack("H*",$sub))." "; # this is the block input
## are we in the last block
if ($j eq $num_blocks){
print "1 ".unpack("B*", pack("N2",$L[$i]))." ";
print unpack("B*",pack("H*",$D[$i]))."\n"; # set last and compare
}
else{
print "0 ".unpack("B*", pack("N2", $j * 512 ))." ".'-' x 256 . "\n"; # don't set last and don't care
}
}
}