skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

sha1_transform.c (1057B)


      1 /* ISC license. */
      2 
      3 #include <stdint.h>
      4 
      5 #include "sha1-internal.h"
      6 
      7 #define F1(x, y, z) ((x & y) | ((~x) & z))
      8 #define F2(x, y, z) (x ^ y ^ z)
      9 #define F3(x, y, z) ((x & y) | (x & z) | (y & z))
     10 #define F4(x, y, z) (x ^ y ^ z)
     11 
     12 #define SHA1STEP(f, data) \
     13 { \
     14   uint32_t tmp = e + f(b, c, d) + data + ((a<<5) | (a>>27)); \
     15   e = d ; \
     16   d = c ; \
     17   c = (b<<30) | (b>>2) ; \
     18   b = a ; \
     19   a = tmp ; \
     20 }
     21 
     22 void sha1_transform (uint32_t *buf, uint32_t const *in)
     23 {
     24   uint32_t a = buf[0], b = buf[1], c = buf[2], d = buf[3], e = buf[4] ;
     25   uint32_t w[80] ;
     26   unsigned int i = 0 ;
     27 
     28   for (; i < 16 ; i++) w[i] = in[i] ;
     29   for (; i < 80 ; i++)
     30   {
     31     w[i] = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ;
     32     w[i] = (w[i]<<1) | (w[i]>>31) ;
     33   }
     34   for (i = 0 ; i < 20 ; i++)
     35     SHA1STEP(F1, w[i] + 0x5a827999U) ;
     36   for (; i < 40 ; i++)
     37     SHA1STEP(F2, w[i] + 0x6ed9eba1U) ;
     38   for (; i < 60 ; i++)
     39     SHA1STEP(F3, w[i] + 0x8f1bbcdcU) ;
     40   for (; i < 80 ; i++)
     41     SHA1STEP(F4, w[i] + 0xca62c1d6U) ;
     42   buf[0] += a ; buf[1] += b ; buf[2] += c ; buf[3] += d ; buf[4] += e ;
     43 }