/*
v0.1 - - Mon Jul 25 14:13:03 CEST 2011
- small modifications for ETH
- Only one hash length
- No extremely long
- No MonteCarlo
- Changed names
ToDo:
- Could add the generation of the random data directly into here
- Drop the silly name and the author stuff
- Perhaps link the .h file to a common name, and not change
*/
#include
#include
#include
#include
#include
/*
#include "blake_kat.h" // Blake
#include "Groestl-ref.h" // Groestl
#include "jh_ref.h" // JH
#include "KeccakDuplex.h" // Keccak
#include "KeccakNISTInterface.h" // Keccak
#include "KeccakSponge.h" // Keccak
#include "SHA3api_ref.h" // Skein
#include "sha3nist.h" // SHA-2 (Pornin)
*/
/* Link all the names to this */
#include "SHA3api_ref.h"
#define MAX_MARKER_LEN 50
#define SUBMITTER_INFO_LEN 128
typedef enum { KAT_SUCCESS = 0, KAT_FILE_OPEN_ERROR = 1, KAT_HEADER_ERROR = 2, KAT_DATA_ERROR = 3, KAT_HASH_ERROR = 4 } STATUS_CODES;
STATUS_CODES genShortMsg(int hashbitlen);
STATUS_CODES genLongMsg(int hashbitlen);
/* STATUS_CODES genExtremelyLongMsg(int hashbitlen); */
/* STATUS_CODES genMonteCarlo(int hashbitlen); */
int FindMarker(FILE *infile, const char *marker);
int ReadHex(FILE *infile, BitSequence *A, int Length, char *str);
void fprintBstr(FILE *fp, char *S, BitSequence *A, int L);
/*STATUS_CODES*/
main()
{
/* Only one value here so bitlens not necessary*/
int i, ret_val;
if ( (ret_val = genLongMsg(256)) != KAT_SUCCESS )
return ret_val;
return KAT_SUCCESS;
}
STATUS_CODES
genLongMsg(int hashbitlen)
{
char fn[32], line[SUBMITTER_INFO_LEN];
int msglen, msgbytelen, done;
/* The length is arbitrary */
BitSequence Msg[16384], MD[64];
FILE *fp_in, *fp_out;
if ( (fp_in = fopen("stimuli.txt", "r")) == NULL ) {
printf("Couldn't open for read\n");
return KAT_FILE_OPEN_ERROR;
}
sprintf(fn, "expresp.txt", hashbitlen);
if ( (fp_out = fopen(fn, "w")) == NULL ) {
printf("Couldn't open <%s> for write\n", fn);
return KAT_FILE_OPEN_ERROR;
}
fprintf(fp_out, "# %s\n", fn);
if ( FindMarker(fp_in, "# Algorithm Name:") ) {
fscanf(fp_in, "%[^\n]\n", line);
fprintf(fp_out, "# Algorithm Name:%s\n", line);
}
else {
printf("genLongMsg: Couldn't read Algorithm Name\n");
return KAT_HEADER_ERROR;
}
if ( FindMarker(fp_in, "# Principal Submitter:") ) {
fscanf(fp_in, "%[^\n]\n", line);
fprintf(fp_out, "# Principal Submitter:%s\n\n", line);
}
else {
printf("genLongMsg: Couldn't read Principal Submitter\n");
return KAT_HEADER_ERROR;
}
done = 0;
do {
if ( FindMarker(fp_in, "Len = ") )
fscanf(fp_in, "%d", &msglen);
else
break;
msgbytelen = (msglen+7)/8;
if ( !ReadHex(fp_in, Msg, msgbytelen, "Msg = ") ) {
printf("ERROR: unable to read 'Msg' from \n");
return KAT_DATA_ERROR;
}
Hash(hashbitlen, Msg, msglen, MD);
fprintf(fp_out, "Len = %d\n", msglen);
fprintBstr(fp_out, "Msg = ", Msg, msgbytelen);
fprintBstr(fp_out, "MD = ", MD, hashbitlen/8);
} while ( !done );
printf("finished LongMsgKAT for <%d>\n", hashbitlen);
fclose(fp_in);
fclose(fp_out);
return KAT_SUCCESS;
}
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int
FindMarker(FILE *infile, const char *marker)
{
char line[MAX_MARKER_LEN];
int i, len;
len = (int)strlen(marker);
if ( len > MAX_MARKER_LEN-1 )
len = MAX_MARKER_LEN-1;
for ( i=0; i= '0') && (ch <= '9') )
ich = ch - '0';
else if ( (ch >= 'A') && (ch <= 'F') )
ich = ch - 'A' + 10;
else if ( (ch >= 'a') && (ch <= 'f') )
ich = ch - 'a' + 10;
for ( i=0; i> 4);
A[Length-1] = (A[Length-1] << 4) | ich;
}
else
return 0;
return 1;
}
void
fprintBstr(FILE *fp, char *S, BitSequence *A, int L)
{
int i;
fprintf(fp, "%s", S);
for ( i=0; i