#! /usr/sepp/bin/perl -w
############################################################
## Copyright: 2011 Integrated Sytems Laboratory, ETH Zurich
## http://www.iis.ee.ethz.ch/~sha3
############################################################
use strict;
# v0.1 - kgf@ee.ethz.ch - Fri Aug 12 14:11:07 CEST 2011
# - adapted from keccak v0.3
# V0.2 - chrikell@ethz.ch - Adapted for FirstBlk Signal
# make sure the lengths are ok later on
## CONSTANTS
my $HASH_BLOCK_SIZE=512; # SHA-256 version
my $MIN_PADDING_SIZE= 72; # this is not completely correct. Min size is
# 65 but it is so much easier to generate this
## 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 SHA2
%% generated by ethz_sha2_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
## Because of the brain dead scheme that these guys interpret
## MSBs 100..0001 actually becomes 01 00 00 80
## 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] -72) / 8;
my $bit_len=sprintf("%016X",$L[$i]); # get the length
my $pad = "80". "00" x $zero_cnt .$bit_len;
## 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 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
# ## generate first block signal
# if ($j eq 1){
# print "1 ";
# }
# else{
# print "0 ";
# }
## are we in the last block
if ($j eq $num_blocks){
print "1 ".unpack("B*",pack("H*",$D[$i]))."\n"; # set last and compare
}
else{
print "0 ". '-' x 256 . "\n"; # don't set last and don't care
}
}
}