/********************************************************************\ * * FILE: hashtest.c * * CONTENTS: test file for sample C-implementation of * RIPEMD-160 and RIPEMD128 * * command line arguments: * filename -- compute hash code of file read binary * -sstring -- print string & hashcode * -t -- perform time trial * -x -- execute standard test suite, ASCII input * * for linkage with rmd128.c: define RMDsize as 128 * for linkage with rmd160.c: define RMDsize as 160 (default) * TARGET: any computer with an ANSI C compiler * * AUTHOR: Antoon Bosselaers, ESAT-COSIC * DATE: 18 April 1996 * VERSION: 1.1 * HISTORY: bug in RMDonemillion() corrected * * Copyright (c) Katholieke Universiteit Leuven * 1996, All Rights Reserved * \********************************************************************/ #ifndef RMDsize #define RMDsize 160 #endif #include #include #include #include #if RMDsize == 128 #include "rmd128.h" #elif RMDsize == 160 #include "rmd160.h" #endif #define TEST_BLOCK_SIZE 8000 #define TEST_BLOCKS 1250 #define TEST_BYTES ((long)TEST_BLOCK_SIZE * (long)TEST_BLOCKS) /********************************************************************/ byte *RMD(byte *message) /* * returns RMD(message) * message should be a string terminated by '\0' */ { dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */ static byte hashcode[RMDsize/8]; /* for final hash-value */ dword X[16]; /* current 16-word chunk */ word i; /* counter */ dword length; /* length in bytes of message */ dword nbytes; /* # of bytes not yet processed */ /* initialize */ MDinit(MDbuf); length = (dword)strlen((char *)message); /* process message in 16-word chunks */ for (nbytes=length; nbytes > 63; nbytes-=64) { for (i=0; i<16; i++) { X[i] = BYTES_TO_DWORD(message); message += 4; } compress(MDbuf, X); } /* length mod 64 bytes left */ /* finish: */ MDfinish(MDbuf, message, length, 0); for (i=0; i>2]; /* implicit cast to byte */ hashcode[i+1] = (MDbuf[i>>2] >> 8); /* extracts the 8 least */ hashcode[i+2] = (MDbuf[i>>2] >> 16); /* significant bits. */ hashcode[i+3] = (MDbuf[i>>2] >> 24); } return (byte *)hashcode; } /********************************************************************/ byte *RMDbinary(char *fname) /* * returns RMD(message in file fname) * fname is read as binary data. */ { FILE *mf; /* pointer to file */ byte data[1024]; /* contains current mess. block */ dword nbytes; /* length of this block */ dword MDbuf[RMDsize/32]; /* contains (A, B, C, D(, E)) */ static byte hashcode[RMDsize/8]; /* for final hash-value */ dword X[16]; /* current 16-word chunk */ word i, j; /* counters */ dword length[2]; /* length in bytes of message */ dword offset; /* # of unprocessed bytes at */ /* call of MDfinish */ /* initialize */ if ((mf = fopen(fname, "rb")) == NULL) { fprintf(stderr, "\nRMDbinary: cannot open file \"%s\".\n", fname); exit(1); } MDinit(MDbuf); length[0] = 0; length[1] = 0; while ((nbytes = fread(data, 1, 1024, mf)) != 0) { /* process all complete blocks */ for (i=0; i<(nbytes>>6); i++) { for (j=0; j<16; j++) X[j] = BYTES_TO_DWORD(data+64*i+4*j); compress(MDbuf, X); } /* update length[] */ if (length[0] + nbytes < length[0]) length[1]++; /* overflow to msb of length */ length[0] += nbytes; } /* finish: */ offset = length[0] & 0x3C0; /* extract bytes 6 to 10 inclusive */ MDfinish(MDbuf, data+offset, length[0], length[1]); for (i=0; i>2]; hashcode[i+1] = (MDbuf[i>>2] >> 8); hashcode[i+2] = (MDbuf[i>>2] >> 16); hashcode[i+3] = (MDbuf[i>>2] >> 24); } fclose(mf); return (byte *)hashcode; } /********************************************************************/ void speedtest(void) /* * A time trial routine, to measure the speed of ripemd. * Measures processor time required to process TEST_BLOCKS times * a message of TEST_BLOCK_SIZE characters. */ { clock_t t0, t1; byte *data; byte hashcode[RMDsize/8]; dword X[16]; dword MDbuf[RMDsize/32]; word i, j, k; srand(time(NULL)); /* allocate and initialize test data */ if ((data = (byte*)malloc(TEST_BLOCK_SIZE)) == NULL) { fprintf(stderr, "speedtest: allocation error\n"); exit(1); } for (i=0; i> 7); /* start timer */ printf("RIPEMD time trial. Processing %ld characters...\n", TEST_BYTES); t0 = clock(); /* process data */ MDinit(MDbuf); for (i=0; i>2]; hashcode[i+1] = (MDbuf[i>>2] >> 8); hashcode[i+2] = (MDbuf[i>>2] >> 16); hashcode[i+3] = (MDbuf[i>>2] >> 24); } printf("\nhashcode: "); for (i=0; i0; i--) compress(MDbuf, X); MDfinish(MDbuf, NULL, 1000000UL, 0); for (i=0; i>2]; hashcode[i+1] = (MDbuf[i>>2] >> 8); hashcode[i+2] = (MDbuf[i>>2] >> 16); hashcode[i+3] = (MDbuf[i>>2] >> 24); } printf("\n* message: 1 million times \"a\"\n hashcode: "); for (i=0; i