Subversion Repositories planix.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 33
Line 1... Line 1...
1
/*
1
/*
2
 * sha2_512 block cipher
2
 * sha2_512 block cipher - unrolled version
3
 *
3
 *
4
 * Implementation straight from Federal Information Processing Standards
-
 
5
 * publication 180-2 (+Change Notice to include SHA-224) August 1, 2002
-
 
6
 *   note: the following upper and lower case macro names are distinct
4
 *   note: the following upper and lower case macro names are distinct
7
 *	   and reflect the functions defined in FIPS pub. 180-2.
5
 *	   and reflect the functions defined in FIPS pub. 180-2.
8
 */
6
 */
-
 
7
 
9
#include <u.h>
8
#include "os.h"
10
#include <libc.h>
-
 
11
 
9
 
12
#define ROTR(x,n)	(((x) >> (n)) | ((x) << (64-(n))))
10
#define ROTR(x,n)	(((x) >> (n)) | ((x) << (64-(n))))
13
#define sigma0(x)	(ROTR((x),1) ^ ROTR((x),8) ^ ((x) >> 7))
11
#define sigma0(x)	(ROTR((x),1) ^ ROTR((x),8) ^ ((x) >> 7))
14
#define sigma1(x)	(ROTR((x),19) ^ ROTR((x),61) ^ ((x) >> 6))
12
#define sigma1(x)	(ROTR((x),19) ^ ROTR((x),61) ^ ((x) >> 6))
15
#define SIGMA0(x)	(ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
13
#define SIGMA0(x)	(ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
16
#define SIGMA1(x)	(ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
14
#define SIGMA1(x)	(ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
17
#define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
15
#define Ch(x,y,z)	((z) ^ ((x) & ((y) ^ (z))))
18
#define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
16
#define Maj(x,y,z)	(((x) | (y)) & ((z) | ((x) & (y))))
19
 
17
 
20
/*
18
/*
21
 * first 64 bits of the fractional parts of cube roots of
19
 * first 64 bits of the fractional parts of cube roots of
22
 * first 80 primes (2..311).
20
 * first 80 primes (2..311).
23
 */
21
 */
24
static u64int K512[80] = {
22
static u64int K512[80] = {
Line 39... Line 37...
39
	0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL,
37
	0x748f82ee5defb2fcLL, 0x78a5636f43172f60LL, 0x84c87814a1f0ab72LL, 0x8cc702081a6439ecLL,
40
	0x90befffa23631e28LL, 0xa4506cebde82bde9LL, 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL,
38
	0x90befffa23631e28LL, 0xa4506cebde82bde9LL, 0xbef9a3f7b2c67915LL, 0xc67178f2e372532bLL,
41
	0xca273eceea26619cLL, 0xd186b8c721c0c207LL, 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL,
39
	0xca273eceea26619cLL, 0xd186b8c721c0c207LL, 0xeada7dd6cde0eb1eLL, 0xf57d4f7fee6ed178LL,
42
	0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, 0x113f9804bef90daeLL, 0x1b710b35131c471bLL,
40
	0x06f067aa72176fbaLL, 0x0a637dc5a2c898a6LL, 0x113f9804bef90daeLL, 0x1b710b35131c471bLL,
43
	0x28db77f523047d84LL, 0x32caab7b40c72493LL, 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL,
41
	0x28db77f523047d84LL, 0x32caab7b40c72493LL, 0x3c9ebe0a15c9bebcLL, 0x431d67c49c100d4cLL,
44
	0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL };
42
	0x4cc5d4becb3e42b6LL, 0x597f299cfc657e2aLL, 0x5fcb6fab3ad6faecLL, 0x6c44198c4a475817LL
-
 
43
};
45
 
44
 
46
void
45
void
47
_sha2block128(uchar *p, ulong len, u64int *s)
46
_sha2block128(uchar *p, ulong len, u64int *s)
48
{
47
{
49
	u64int a, b, c, d, e, f, g, h, t1, t2;
48
	u64int w[16], a, b, c, d, e, f, g, h;
50
	u64int *kp, *wp;
-
 
51
	u64int w[80];
-
 
52
	uchar *end;
49
	uchar *end;
53
 
50
 
54
	/* at this point, we have a multiple of 64 bytes */
51
	/* at this point, we have a multiple of 64 bytes */
55
	for(end = p+len; p < end;){
52
	for(end = p+len; p < end;){
56
		a = s[0];
53
		a = s[0];
Line 60... Line 57...
60
		e = s[4];
57
		e = s[4];
61
		f = s[5];
58
		f = s[5];
62
		g = s[6];
59
		g = s[6];
63
		h = s[7];
60
		h = s[7];
64
 
61
 
65
		for(wp = w; wp < &w[16]; wp++, p += 8)
62
#define STEP(a,b,c,d,e,f,g,h,i) \
66
			wp[0] = ((vlong)p[0])<<56 | ((vlong)p[1])<<48 |
63
	if(i < 16) { \
67
				((vlong)p[2])<<40 | ((vlong)p[3])<<32 |
64
		w[i] = 	(u64int)(p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3])<<32 | \
68
				p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7];
65
			(p[4]<<24 | p[5]<<16 | p[6]<<8 | p[7]); \
69
		for(; wp < &w[80]; wp++) {
66
		p += 8; \
-
 
67
	} else { \
70
			u64int s0, s1;
68
		u64int s0, s1; \
-
 
69
		s1 = sigma1(w[i-2&15]); \
-
 
70
		s0 = sigma0(w[i-15&15]); \
-
 
71
		w[i&15] += s1 + w[i-7&15] + s0; \
-
 
72
	} \
-
 
73
	h += SIGMA1(e) + Ch(e,f,g) + K512[i] + w[i&15]; \
-
 
74
	d += h; \
-
 
75
	h += SIGMA0(a) + Maj(a,b,c);
-
 
76
 
-
 
77
		STEP(a,b,c,d,e,f,g,h,0);
-
 
78
		STEP(h,a,b,c,d,e,f,g,1);
-
 
79
		STEP(g,h,a,b,c,d,e,f,2);
-
 
80
		STEP(f,g,h,a,b,c,d,e,3);
-
 
81
		STEP(e,f,g,h,a,b,c,d,4);
-
 
82
		STEP(d,e,f,g,h,a,b,c,5);
-
 
83
		STEP(c,d,e,f,g,h,a,b,6);
-
 
84
		STEP(b,c,d,e,f,g,h,a,7);
-
 
85
 
-
 
86
		STEP(a,b,c,d,e,f,g,h,8);
-
 
87
		STEP(h,a,b,c,d,e,f,g,9);
-
 
88
		STEP(g,h,a,b,c,d,e,f,10);
-
 
89
		STEP(f,g,h,a,b,c,d,e,11);
-
 
90
		STEP(e,f,g,h,a,b,c,d,12);
-
 
91
		STEP(d,e,f,g,h,a,b,c,13);
-
 
92
		STEP(c,d,e,f,g,h,a,b,14);
-
 
93
		STEP(b,c,d,e,f,g,h,a,15);
-
 
94
 
-
 
95
		STEP(a,b,c,d,e,f,g,h,16);
-
 
96
		STEP(h,a,b,c,d,e,f,g,17);
-
 
97
		STEP(g,h,a,b,c,d,e,f,18);
-
 
98
		STEP(f,g,h,a,b,c,d,e,19);
-
 
99
		STEP(e,f,g,h,a,b,c,d,20);
-
 
100
		STEP(d,e,f,g,h,a,b,c,21);
-
 
101
		STEP(c,d,e,f,g,h,a,b,22);
-
 
102
		STEP(b,c,d,e,f,g,h,a,23);
-
 
103
 
-
 
104
		STEP(a,b,c,d,e,f,g,h,24);
-
 
105
		STEP(h,a,b,c,d,e,f,g,25);
-
 
106
		STEP(g,h,a,b,c,d,e,f,26);
-
 
107
		STEP(f,g,h,a,b,c,d,e,27);
-
 
108
		STEP(e,f,g,h,a,b,c,d,28);
-
 
109
		STEP(d,e,f,g,h,a,b,c,29);
-
 
110
		STEP(c,d,e,f,g,h,a,b,30);
-
 
111
		STEP(b,c,d,e,f,g,h,a,31);
-
 
112
 
-
 
113
		STEP(a,b,c,d,e,f,g,h,32);
-
 
114
		STEP(h,a,b,c,d,e,f,g,33);
-
 
115
		STEP(g,h,a,b,c,d,e,f,34);
-
 
116
		STEP(f,g,h,a,b,c,d,e,35);
-
 
117
		STEP(e,f,g,h,a,b,c,d,36);
-
 
118
		STEP(d,e,f,g,h,a,b,c,37);
-
 
119
		STEP(c,d,e,f,g,h,a,b,38);
-
 
120
		STEP(b,c,d,e,f,g,h,a,39);
-
 
121
 
-
 
122
		STEP(a,b,c,d,e,f,g,h,40);
-
 
123
		STEP(h,a,b,c,d,e,f,g,41);
-
 
124
		STEP(g,h,a,b,c,d,e,f,42);
-
 
125
		STEP(f,g,h,a,b,c,d,e,43);
-
 
126
		STEP(e,f,g,h,a,b,c,d,44);
-
 
127
		STEP(d,e,f,g,h,a,b,c,45);
-
 
128
		STEP(c,d,e,f,g,h,a,b,46);
-
 
129
		STEP(b,c,d,e,f,g,h,a,47);
-
 
130
 
-
 
131
		STEP(a,b,c,d,e,f,g,h,48);
-
 
132
		STEP(h,a,b,c,d,e,f,g,49);
-
 
133
		STEP(g,h,a,b,c,d,e,f,50);
-
 
134
		STEP(f,g,h,a,b,c,d,e,51);
-
 
135
		STEP(e,f,g,h,a,b,c,d,52);
-
 
136
		STEP(d,e,f,g,h,a,b,c,53);
-
 
137
		STEP(c,d,e,f,g,h,a,b,54);
-
 
138
		STEP(b,c,d,e,f,g,h,a,55);
-
 
139
 
-
 
140
		STEP(a,b,c,d,e,f,g,h,56);
-
 
141
		STEP(h,a,b,c,d,e,f,g,57);
-
 
142
		STEP(g,h,a,b,c,d,e,f,58);
-
 
143
		STEP(f,g,h,a,b,c,d,e,59);
-
 
144
		STEP(e,f,g,h,a,b,c,d,60);
-
 
145
		STEP(d,e,f,g,h,a,b,c,61);
-
 
146
		STEP(c,d,e,f,g,h,a,b,62);
-
 
147
		STEP(b,c,d,e,f,g,h,a,63);
71
 
148
 
-
 
149
		STEP(a,b,c,d,e,f,g,h,64);
-
 
150
		STEP(h,a,b,c,d,e,f,g,65);
-
 
151
		STEP(g,h,a,b,c,d,e,f,66);
72
			s0 = sigma0(wp[-15]);
152
		STEP(f,g,h,a,b,c,d,e,67);
73
			s1 = sigma1(wp[-2]);
153
		STEP(e,f,g,h,a,b,c,d,68);
74
//			wp[0] = sigma1(wp[-2]) + wp[-7] + sigma0(wp[-15]) + wp[-16];
154
		STEP(d,e,f,g,h,a,b,c,69);
75
			wp[0] = s1 + wp[-7] + s0 + wp[-16];
155
		STEP(c,d,e,f,g,h,a,b,70);
76
		}
156
		STEP(b,c,d,e,f,g,h,a,71);
77
 
157
 
78
		for(kp = K512, wp = w; wp < &w[80]; ) {
158
		STEP(a,b,c,d,e,f,g,h,72);
79
			t1 = h + SIGMA1(e) + Ch(e,f,g) + *kp++ + *wp++;
159
		STEP(h,a,b,c,d,e,f,g,73);
80
			t2 = SIGMA0(a) + Maj(a,b,c);
160
		STEP(g,h,a,b,c,d,e,f,74);
81
			h = g;
-
 
82
			g = f;
-
 
83
			f = e;
161
		STEP(f,g,h,a,b,c,d,e,75);
84
			e = d + t1;
162
		STEP(e,f,g,h,a,b,c,d,76);
85
			d = c;
163
		STEP(d,e,f,g,h,a,b,c,77);
86
			c = b;
164
		STEP(c,d,e,f,g,h,a,b,78);
87
			b = a;
-
 
88
			a = t1 + t2;
165
		STEP(b,c,d,e,f,g,h,a,79);
89
		}
-
 
90
 
166
 
91
		/* save state */
-
 
92
		s[0] += a;
167
		s[0] += a;
93
		s[1] += b;
168
		s[1] += b;
94
		s[2] += c;
169
		s[2] += c;
95
		s[3] += d;
170
		s[3] += d;
96
		s[4] += e;
171
		s[4] += e;