sha1.c revision 4af48bf7c72ef1e201c64bd475377b5af9d8e8a1
/* from valgrind tests */
/* ================ sha1.c ================ */
/*
SHA-1 in C
By Steve Reid <steve@edmweb.com>
100% Public Domain
Test Vectors (from FIPS PUB 180-1)
"abc"
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
A million repetitions of "a"
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
/* #define SHA1HANDSOFF * Copies data before messing with it. */
#define SHA1HANDSOFF
#include <stdio.h>
#include <string.h>
/* ================ sha1.h ================ */
/*
SHA-1 in C
By Steve Reid <steve@edmweb.com>
100% Public Domain
*/
typedef struct {
unsigned char buffer[64];
} SHA1_CTX;
/* ================ end of sha1.h ================ */
#include <endian.h>
/* blk0() and blk() perform the initial expand. */
/* I got the idea of expanding during the round function from SSLeay */
#if BYTE_ORDER == LITTLE_ENDIAN
#else
#error "Endianness not defined!"
#endif
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
/* Hash a single 512-bit block. This is the core of the algorithm. */
{
u_int32_t a, b, c, d, e;
typedef union {
unsigned char c[64];
u_int32_t l[16];
} CHAR64LONG16;
#ifdef SHA1HANDSOFF
#else
/* The following had better never be used because it causes the
* pointer-to-const buffer to be cast into a pointer to non-const.
* And the result is written through. I threw a "const" in, hoping
* this will cause a diagnostic.
*/
#endif
/* Copy context->state[] to working vars */
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
/* 4 rounds of 20 operations each. Loop unrolled. */
/* Add the working vars back into context.state[] */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
/* Wipe variables */
a = b = c = d = e = 0;
#ifdef SHA1HANDSOFF
#endif
}
/* SHA1Init - Initialize new context */
{
/* SHA1 initialization constants */
}
/* Run your data through this. */
{
u_int32_t i;
u_int32_t j;
j = (j >> 3) & 63;
if ((j + len) > 63) {
}
j = 0;
}
else i = 0;
}
/* Add padding and return the message digest. */
{
unsigned i;
unsigned char finalcount[8];
unsigned char c;
#if 0 /* untested "improvement" by DHR */
/* Convert context->count to a sequence of bytes
* in finalcount. Second element first, but
* big-endian order within element.
* But we do it all backwards.
*/
for (i = 0; i < 2; i++)
{
int j;
for (j = 0; j < 4; t >>= 8, j++)
*--fcp = (unsigned char) t
}
#else
for (i = 0; i < 8; i++) {
>> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
}
#endif
c = 0200;
c = 0000;
}
for (i = 0; i < 20; i++) {
digest[i] = (unsigned char)
}
/* Wipe variables */
}
/* ================ end of sha1.c ================ */
#define BUFSIZE 4096
int
{
int i;
for(i=0;i<BUFSIZE;i++)
buf[i] = i;
for(i=0;i<1000;i++)
printf("SHA1=");
for(i=0;i<20;i++)
printf("\n");
return 0;
}