home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
nisttime.carsoncity.k12.mi.us
/
nisttime.carsoncity.k12.mi.us.tar
/
nisttime.carsoncity.k12.mi.us
/
pub
/
sha
/
sha.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-06-29
|
2KB
|
100 lines
#include <stdio.h>
#include "sizint.h"
/*
this subroutine computes the message digest of an input
string following the algorithm described in FIPS 180,
version of 11 May 1993.
the notation used here is taken from the FIPS document
the message is broken into 16-word blocks called m1, ...
the elements of each 16-word block are called w[0],...
where the first element holds the start of the message, etc
the detailed construction of the blocks as described in the
FIPS document is performed by the caller.
*/
ULONG h[5];
ULONG k[4]; /* the four distinct values of k*/
void sha(ULONG w[16],ULONG ans[5])
{
ULONG a,b,c,d,e;
ULONG temp;
ULONG t2;
ULONG ww[80]; /*local arrau for expanded w*/
ULONG s5(ULONG); /*circular left shift 5 bits*/
ULONG s30(ULONG); /*circular left shift 30 bits*/
ULONG f0(ULONG, ULONG, ULONG);
ULONG f1(ULONG, ULONG, ULONG);
ULONG f2(ULONG, ULONG, ULONG);
ULONG f3(ULONG, ULONG, ULONG);
int i;
/*
copy w into ww and expand ww, FIPS 180, p. 9
note that the modified FIPS 180 adds a 1-bit circular
left shift to the expansion of ww. see fips 180-1,
page 8.
*/
for(i=0; i<16; i++) ww[i]=w[i];
for(i=16; i<80; i++)
{
temp=ww[i-3] ^ ww[i-8] ^ ww[i-14] ^ ww[i-16];
t2= temp & 0x80000000u;
temp = temp << 1;
if(t2 != 0) temp |= 1; /*add it least sig bit if needed*/
ww[i]= temp;
}
a=h[0];
b=h[1];
c=h[2];
d=h[3];
e=h[4];
/*
the 80 rounds of the sha use 4 different functions but are
otherwise identical. the function changes are implemented
by breaking the 80 rounds into 4 groups of 20 each and
changing the function name and the k value in each group.
*/
for(i=0; i<20; i++)
{
temp=s5(a) + f0(b,c,d) + e + ww[i] + k[0];
e=d;
d=c;
c=s30(b);
b=a;
a=temp;
}
for(i=20; i<40; i++)
{
temp=s5(a) + f1(b,c,d) + e + ww[i] + k[1];
e=d;
d=c;
c=s30(b);
b=a;
a=temp;
}
for(i=40; i<60; i++)
{
temp=s5(a) + f2(b,c,d) + e +ww[i] + k[2];
e=d;
d=c;
c=s30(b);
b=a;
a=temp;
}
for(i=60; i<80; i++)
{
temp=s5(a) + f3(b,c,d) + e + ww[i] + k[3];
e=d;
d=c;
c=s30(b);
b=a;
a=temp;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
for(i=0; i<5; i++) ans[i]=h[i]; /*return answer */
}