Subversion Repositories planix.SVN

Rev

Rev 33 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
/*
33 7u83 2
 * sha2_256 block cipher - unrolled version
2 - 3
 *
4
 *   note: the following upper and lower case macro names are distinct
5
 *	   and reflect the functions defined in FIPS pub. 180-2.
6
 */
7
 
33 7u83 8
#include "os.h"
2 - 9
 
10
#define ROTR(x,n)	(((x) >> (n)) | ((x) << (32-(n))))
11
#define sigma0(x)	(ROTR((x),7) ^ ROTR((x),18) ^ ((x) >> 3))
12
#define sigma1(x)	(ROTR((x),17) ^ ROTR((x),19) ^ ((x) >> 10))
13
#define SIGMA0(x)	(ROTR((x),2) ^ ROTR((x),13) ^ ROTR((x),22))
14
#define SIGMA1(x)	(ROTR((x),6) ^ ROTR((x),11) ^ ROTR((x),25))
33 7u83 15
#define Ch(x,y,z)	((z) ^ ((x) & ((y) ^ (z))))
16
#define Maj(x,y,z)	(((x) | (y)) & ((z) | ((x) & (y))))
2 - 17
 
18
/*
19
 * first 32 bits of the fractional parts of cube roots of
20
 * first 64 primes (2..311).
21
 */
22
static u32int K256[64] = {
23
	0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,
24
	0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
25
	0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,
26
	0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
27
	0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,
28
	0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
29
	0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,
30
	0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
31
	0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,
32
	0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
33
	0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,
34
	0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
35
	0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,
36
	0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
37
	0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,
38
	0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2,
39
};
40
 
41
void
42
_sha2block64(uchar *p, ulong len, u32int *s)
43
{
33 7u83 44
	u32int w[16], a, b, c, d, e, f, g, h;
2 - 45
	uchar *end;
46
 
47
	/* at this point, we have a multiple of 64 bytes */
48
	for(end = p+len; p < end;){
49
		a = s[0];
50
		b = s[1];
51
		c = s[2];
52
		d = s[3];
53
		e = s[4];
54
		f = s[5];
55
		g = s[6];
56
		h = s[7];
57
 
33 7u83 58
#define STEP(a,b,c,d,e,f,g,h,i) \
59
	if(i < 16) {\
60
		w[i] = p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]; \
61
		p += 4; \
62
	} else { \
63
		w[i&15] += sigma1(w[i-2&15]) + w[i-7&15] + sigma0(w[i-15&15]); \
64
	} \
65
	h += SIGMA1(e) + Ch(e,f,g) + K256[i] + w[i&15]; \
66
	d += h; \
67
	h += SIGMA0(a) + Maj(a,b,c);
2 - 68
 
33 7u83 69
		STEP(a,b,c,d,e,f,g,h,0);
70
		STEP(h,a,b,c,d,e,f,g,1);
71
		STEP(g,h,a,b,c,d,e,f,2);
72
		STEP(f,g,h,a,b,c,d,e,3);
73
		STEP(e,f,g,h,a,b,c,d,4);
74
		STEP(d,e,f,g,h,a,b,c,5);
75
		STEP(c,d,e,f,g,h,a,b,6);
76
		STEP(b,c,d,e,f,g,h,a,7);
2 - 77
 
33 7u83 78
		STEP(a,b,c,d,e,f,g,h,8);
79
		STEP(h,a,b,c,d,e,f,g,9);
80
		STEP(g,h,a,b,c,d,e,f,10);
81
		STEP(f,g,h,a,b,c,d,e,11);
82
		STEP(e,f,g,h,a,b,c,d,12);
83
		STEP(d,e,f,g,h,a,b,c,13);
84
		STEP(c,d,e,f,g,h,a,b,14);
85
		STEP(b,c,d,e,f,g,h,a,15);
86
 
87
		STEP(a,b,c,d,e,f,g,h,16);
88
		STEP(h,a,b,c,d,e,f,g,17);
89
		STEP(g,h,a,b,c,d,e,f,18);
90
		STEP(f,g,h,a,b,c,d,e,19);
91
		STEP(e,f,g,h,a,b,c,d,20);
92
		STEP(d,e,f,g,h,a,b,c,21);
93
		STEP(c,d,e,f,g,h,a,b,22);
94
		STEP(b,c,d,e,f,g,h,a,23);
95
 
96
		STEP(a,b,c,d,e,f,g,h,24);
97
		STEP(h,a,b,c,d,e,f,g,25);
98
		STEP(g,h,a,b,c,d,e,f,26);
99
		STEP(f,g,h,a,b,c,d,e,27);
100
		STEP(e,f,g,h,a,b,c,d,28);
101
		STEP(d,e,f,g,h,a,b,c,29);
102
		STEP(c,d,e,f,g,h,a,b,30);
103
		STEP(b,c,d,e,f,g,h,a,31);
104
 
105
		STEP(a,b,c,d,e,f,g,h,32);
106
		STEP(h,a,b,c,d,e,f,g,33);
107
		STEP(g,h,a,b,c,d,e,f,34);
108
		STEP(f,g,h,a,b,c,d,e,35);
109
		STEP(e,f,g,h,a,b,c,d,36);
110
		STEP(d,e,f,g,h,a,b,c,37);
111
		STEP(c,d,e,f,g,h,a,b,38);
112
		STEP(b,c,d,e,f,g,h,a,39);
113
 
114
		STEP(a,b,c,d,e,f,g,h,40);
115
		STEP(h,a,b,c,d,e,f,g,41);
116
		STEP(g,h,a,b,c,d,e,f,42);
117
		STEP(f,g,h,a,b,c,d,e,43);
118
		STEP(e,f,g,h,a,b,c,d,44);
119
		STEP(d,e,f,g,h,a,b,c,45);
120
		STEP(c,d,e,f,g,h,a,b,46);
121
		STEP(b,c,d,e,f,g,h,a,47);
122
 
123
		STEP(a,b,c,d,e,f,g,h,48);
124
		STEP(h,a,b,c,d,e,f,g,49);
125
		STEP(g,h,a,b,c,d,e,f,50);
126
		STEP(f,g,h,a,b,c,d,e,51);
127
		STEP(e,f,g,h,a,b,c,d,52);
128
		STEP(d,e,f,g,h,a,b,c,53);
129
		STEP(c,d,e,f,g,h,a,b,54);
130
		STEP(b,c,d,e,f,g,h,a,55);
131
 
132
		STEP(a,b,c,d,e,f,g,h,56);
133
		STEP(h,a,b,c,d,e,f,g,57);
134
		STEP(g,h,a,b,c,d,e,f,58);
135
		STEP(f,g,h,a,b,c,d,e,59);
136
		STEP(e,f,g,h,a,b,c,d,60);
137
		STEP(d,e,f,g,h,a,b,c,61);
138
		STEP(c,d,e,f,g,h,a,b,62);
139
		STEP(b,c,d,e,f,g,h,a,63);
140
 
2 - 141
		s[0] += a;
142
		s[1] += b;
143
		s[2] += c;
144
		s[3] += d;
145
		s[4] += e;
146
		s[5] += f;
147
		s[6] += g;
148
		s[7] += h;
149
	}
150
}