9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

sha1block.c (4673B)


      1 #include "os.h"
      2 
      3 void
      4 _sha1block(uchar *p, ulong len, u32int *s)
      5 {
      6 	u32int a, b, c, d, e, x;
      7 	uchar *end;
      8 	u32int *wp, *wend;
      9 	u32int w[80];
     10 
     11 	/* at this point, we have a multiple of 64 bytes */
     12 	for(end = p+len; p < end;){
     13 		a = s[0];
     14 		b = s[1];
     15 		c = s[2];
     16 		d = s[3];
     17 		e = s[4];
     18 
     19 		wend = w + 15;
     20 		for(wp = w; wp < wend; wp += 5){
     21 			wp[0] = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
     22 			e += ((a<<5) | (a>>27)) + wp[0];
     23 			e += 0x5a827999 + (((c^d)&b)^d);
     24 			b = (b<<30)|(b>>2);
     25 
     26 			wp[1] = (p[4]<<24) | (p[5]<<16) | (p[6]<<8) | p[7];
     27 			d += ((e<<5) | (e>>27)) + wp[1];
     28 			d += 0x5a827999 + (((b^c)&a)^c);
     29 			a = (a<<30)|(a>>2);
     30 
     31 			wp[2] = (p[8]<<24) | (p[9]<<16) | (p[10]<<8) | p[11];
     32 			c += ((d<<5) | (d>>27)) + wp[2];
     33 			c += 0x5a827999 + (((a^b)&e)^b);
     34 			e = (e<<30)|(e>>2);
     35 
     36 			wp[3] = (p[12]<<24) | (p[13]<<16) | (p[14]<<8) | p[15];
     37 			b += ((c<<5) | (c>>27)) + wp[3];
     38 			b += 0x5a827999 + (((e^a)&d)^a);
     39 			d = (d<<30)|(d>>2);
     40 
     41 			wp[4] = (p[16]<<24) | (p[17]<<16) | (p[18]<<8) | p[19];
     42 			a += ((b<<5) | (b>>27)) + wp[4];
     43 			a += 0x5a827999 + (((d^e)&c)^e);
     44 			c = (c<<30)|(c>>2);
     45 			
     46 			p += 20;
     47 		}
     48 
     49 		wp[0] = (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
     50 		e += ((a<<5) | (a>>27)) + wp[0];
     51 		e += 0x5a827999 + (((c^d)&b)^d);
     52 		b = (b<<30)|(b>>2);
     53 
     54 		x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
     55 		wp[1] = (x<<1) | (x>>31);
     56 		d += ((e<<5) | (e>>27)) + wp[1];
     57 		d += 0x5a827999 + (((b^c)&a)^c);
     58 		a = (a<<30)|(a>>2);
     59 
     60 		x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
     61 		wp[2] = (x<<1) | (x>>31);
     62 		c += ((d<<5) | (d>>27)) + wp[2];
     63 		c += 0x5a827999 + (((a^b)&e)^b);
     64 		e = (e<<30)|(e>>2);
     65 
     66 		x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
     67 		wp[3] = (x<<1) | (x>>31);
     68 		b += ((c<<5) | (c>>27)) + wp[3];
     69 		b += 0x5a827999 + (((e^a)&d)^a);
     70 		d = (d<<30)|(d>>2);
     71 
     72 		x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
     73 		wp[4] = (x<<1) | (x>>31);
     74 		a += ((b<<5) | (b>>27)) + wp[4];
     75 		a += 0x5a827999 + (((d^e)&c)^e);
     76 		c = (c<<30)|(c>>2);
     77 
     78 		wp += 5;
     79 		p += 4;
     80 
     81 		wend = w + 40;
     82 		for(; wp < wend; wp += 5){
     83 			x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
     84 			wp[0] = (x<<1) | (x>>31);
     85 			e += ((a<<5) | (a>>27)) + wp[0];
     86 			e += 0x6ed9eba1 + (b^c^d);
     87 			b = (b<<30)|(b>>2);
     88 
     89 			x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
     90 			wp[1] = (x<<1) | (x>>31);
     91 			d += ((e<<5) | (e>>27)) + wp[1];
     92 			d += 0x6ed9eba1 + (a^b^c);
     93 			a = (a<<30)|(a>>2);
     94 
     95 			x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
     96 			wp[2] = (x<<1) | (x>>31);
     97 			c += ((d<<5) | (d>>27)) + wp[2];
     98 			c += 0x6ed9eba1 + (e^a^b);
     99 			e = (e<<30)|(e>>2);
    100 
    101 			x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
    102 			wp[3] = (x<<1) | (x>>31);
    103 			b += ((c<<5) | (c>>27)) + wp[3];
    104 			b += 0x6ed9eba1 + (d^e^a);
    105 			d = (d<<30)|(d>>2);
    106 
    107 			x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
    108 			wp[4] = (x<<1) | (x>>31);
    109 			a += ((b<<5) | (b>>27)) + wp[4];
    110 			a += 0x6ed9eba1 + (c^d^e);
    111 			c = (c<<30)|(c>>2);
    112 		}
    113 
    114 		wend = w + 60;
    115 		for(; wp < wend; wp += 5){
    116 			x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
    117 			wp[0] = (x<<1) | (x>>31);
    118 			e += ((a<<5) | (a>>27)) + wp[0];
    119 			e += 0x8f1bbcdc + ((b&c)|((b|c)&d));
    120 			b = (b<<30)|(b>>2);
    121 
    122 			x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
    123 			wp[1] = (x<<1) | (x>>31);
    124 			d += ((e<<5) | (e>>27)) + wp[1];
    125 			d += 0x8f1bbcdc + ((a&b)|((a|b)&c));
    126 			a = (a<<30)|(a>>2);
    127 
    128 			x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
    129 			wp[2] = (x<<1) | (x>>31);
    130 			c += ((d<<5) | (d>>27)) + wp[2];
    131 			c += 0x8f1bbcdc + ((e&a)|((e|a)&b));
    132 			e = (e<<30)|(e>>2);
    133 
    134 			x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
    135 			wp[3] = (x<<1) | (x>>31);
    136 			b += ((c<<5) | (c>>27)) + wp[3];
    137 			b += 0x8f1bbcdc + ((d&e)|((d|e)&a));
    138 			d = (d<<30)|(d>>2);
    139 
    140 			x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
    141 			wp[4] = (x<<1) | (x>>31);
    142 			a += ((b<<5) | (b>>27)) + wp[4];
    143 			a += 0x8f1bbcdc + ((c&d)|((c|d)&e));
    144 			c = (c<<30)|(c>>2);
    145 		}
    146 
    147 		wend = w + 80;
    148 		for(; wp < wend; wp += 5){
    149 			x = wp[-3] ^ wp[-8] ^ wp[-14] ^ wp[-16];
    150 			wp[0] = (x<<1) | (x>>31);
    151 			e += ((a<<5) | (a>>27)) + wp[0];
    152 			e += 0xca62c1d6 + (b^c^d);
    153 			b = (b<<30)|(b>>2);
    154 
    155 			x = wp[-2] ^ wp[-7] ^ wp[-13] ^ wp[-15];
    156 			wp[1] = (x<<1) | (x>>31);
    157 			d += ((e<<5) | (e>>27)) + wp[1];
    158 			d += 0xca62c1d6 + (a^b^c);
    159 			a = (a<<30)|(a>>2);
    160 
    161 			x = wp[-1] ^ wp[-6] ^ wp[-12] ^ wp[-14];
    162 			wp[2] = (x<<1) | (x>>31);
    163 			c += ((d<<5) | (d>>27)) + wp[2];
    164 			c += 0xca62c1d6 + (e^a^b);
    165 			e = (e<<30)|(e>>2);
    166 
    167 			x = wp[0] ^ wp[-5] ^ wp[-11] ^ wp[-13];
    168 			wp[3] = (x<<1) | (x>>31);
    169 			b += ((c<<5) | (c>>27)) + wp[3];
    170 			b += 0xca62c1d6 + (d^e^a);
    171 			d = (d<<30)|(d>>2);
    172 
    173 			x = wp[1] ^ wp[-4] ^ wp[-10] ^ wp[-12];
    174 			wp[4] = (x<<1) | (x>>31);
    175 			a += ((b<<5) | (b>>27)) + wp[4];
    176 			a += 0xca62c1d6 + (c^d^e);
    177 			c = (c<<30)|(c>>2);
    178 		}
    179 
    180 		/* save state */
    181 		s[0] += a;
    182 		s[1] += b;
    183 		s[2] += c;
    184 		s[3] += d;
    185 		s[4] += e;
    186 	}
    187 }