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 >
C/C++ Source or Header  |  2001-06-29  |  2KB  |  100 lines

  1. #include  <stdio.h>
  2. #include "sizint.h"
  3. /*
  4.     this subroutine computes the message digest of an input
  5.     string following the algorithm described in FIPS 180,
  6.     version of 11 May 1993.
  7.     the notation used here is taken from the FIPS document
  8.  
  9.     the message is broken into 16-word blocks called m1, ...
  10.     the elements of each 16-word block are called w[0],...
  11.     where the first element holds the start of the message, etc
  12.     the detailed construction of the blocks as described in the
  13.     FIPS document is performed by the caller.
  14. */
  15. ULONG h[5];
  16. ULONG k[4];        /* the four distinct values of k*/
  17. void sha(ULONG w[16],ULONG ans[5])
  18. {
  19. ULONG a,b,c,d,e;
  20. ULONG temp;
  21. ULONG t2;
  22. ULONG ww[80];    /*local arrau for expanded w*/
  23. ULONG s5(ULONG);     /*circular left shift 5 bits*/
  24. ULONG s30(ULONG);    /*circular left shift 30 bits*/
  25. ULONG f0(ULONG, ULONG, ULONG);
  26. ULONG f1(ULONG, ULONG, ULONG);
  27. ULONG f2(ULONG, ULONG, ULONG);
  28. ULONG f3(ULONG, ULONG, ULONG);
  29. int i;
  30. /*
  31.     copy w into ww and expand ww, FIPS 180, p. 9
  32.  
  33.     note that the modified FIPS 180 adds a 1-bit circular
  34.     left shift to the expansion of ww.  see fips 180-1,
  35.     page 8.
  36. */
  37.     for(i=0; i<16; i++) ww[i]=w[i];
  38.     for(i=16; i<80; i++) 
  39.        {
  40.        temp=ww[i-3] ^ ww[i-8] ^ ww[i-14] ^ ww[i-16];
  41.        t2= temp & 0x80000000u;
  42.        temp = temp << 1;
  43.        if(t2 != 0) temp |= 1;    /*add it least sig bit if needed*/
  44.        ww[i]= temp;
  45.     }
  46.     a=h[0];
  47.     b=h[1];
  48.     c=h[2];
  49.     d=h[3];
  50.     e=h[4];
  51. /*
  52.     the 80 rounds of the sha use 4 different functions but are
  53.     otherwise identical.  the function changes are implemented
  54.     by breaking the 80 rounds into 4 groups of 20 each and
  55.     changing the function name and the k value in each group.
  56. */
  57.     for(i=0; i<20; i++)
  58.         {
  59.         temp=s5(a) + f0(b,c,d) + e + ww[i] + k[0];
  60.         e=d;
  61.         d=c;
  62.         c=s30(b);
  63.         b=a;
  64.         a=temp;
  65.     }
  66.     for(i=20; i<40; i++)
  67.         {
  68.         temp=s5(a) + f1(b,c,d) + e + ww[i] + k[1];
  69.         e=d;
  70.         d=c;
  71.         c=s30(b);
  72.         b=a;
  73.         a=temp;
  74.     }
  75.     for(i=40; i<60; i++)
  76.         {
  77.         temp=s5(a) + f2(b,c,d) + e +ww[i] + k[2];
  78.         e=d;
  79.         d=c;
  80.         c=s30(b);
  81.         b=a;
  82.         a=temp;
  83.     }
  84.     for(i=60; i<80; i++)
  85.         {
  86.         temp=s5(a) + f3(b,c,d) + e + ww[i] + k[3];
  87.         e=d;
  88.         d=c;
  89.         c=s30(b);
  90.         b=a;
  91.         a=temp;
  92.     }
  93.     h[0] += a;
  94.     h[1] += b;
  95.     h[2] += c;
  96.     h[3] += d;
  97.     h[4] += e;
  98.     for(i=0; i<5; i++) ans[i]=h[i];    /*return answer */
  99. }
  100.