aboutsummaryrefslogtreecommitdiff
path: root/cl/onion.cl
blob: e6835b9a2d02ed39692eb656dfe3e6dfc400f55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#define INITIAL_DATA_LEN 9
#define SHA_CHUNK_LEN 64
#define ROL(x, shamt) ((x << shamt) | (x >> (sizeof(x)*8 - shamt)))

struct sha_data {
	unsigned int a;
	unsigned int b;
	unsigned int c;
	unsigned int d;
	unsigned int e;
	unsigned long len;
	unsigned long data_len;
	unsigned char data[SHA_CHUNK_LEN];
};

void sha_chunk(unsigned char (*buf)[SHA_CHUNK_LEN], struct sha_data *sha) {
	unsigned int w[80] = {0};
	unsigned int new_a = 0;
	unsigned int a = sha->a;
	unsigned int b = sha->b;
	unsigned int c = sha->c;
	unsigned int d = sha->d;
	unsigned int e = sha->e;
	unsigned int i = 0;
	unsigned int bo = 0;

	const unsigned int k[] = {
		0x5A827999,
		0x6ED9EBA1,
		0x8F1BBCDC,
		0xCA62C1D6
	};

	#pragma unroll
	for (i = 0; i < 16; i++, bo+=4) {
		w[i] = ((*buf)[bo]) << 24;
		w[i] |= ((*buf)[bo+1]) << 16;
		w[i] |= ((*buf)[bo+2]) << 8;
		w[i] |= ((*buf)[bo+3]);
	}

//	#pragma unroll
	for (i = 16; i < 80; i++) {
		w[i] = ROL((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
	}

//	#pragma unroll
	for (i = 0; i < 20; i++) {
		new_a = ROL(a, 5) + ((b&c)|((~b)&d)) + e + w[i] + k[0];
		e = d;
		d = c;
		c = ROL(b, 30);
		b = a;
		a = new_a;
	}

//	#pragma unroll
	for (i = 20; i < 40; i++) {
		new_a = ROL(a, 5) + (b^c^d) + e + w[i] + k[1];
		e = d;
		d = c;
		c = ROL(b, 30);
		b = a;
		a = new_a;
	}

//	#pragma unroll
	for (i = 40; i < 60; i++) {
		new_a = ROL(a, 5) + ((b&c)|(b&d)|(c&d)) + e + w[i] + k[2];
		e = d;
		d = c;
		c = ROL(b, 30);
		b = a;
		a = new_a;
	}
//	#pragma unroll
	for (i = 60; i < 80; i++) {
		new_a = ROL(a, 5) + (b^c^d) + e + w[i] + k[3];
		e = d;
		d = c;
		c = ROL(b, 30);
		b = a;
		a = new_a;
	}
	sha->a += a;
	sha->b += b;
	sha->c += c;
	sha->d += d;
	sha->e += e;
}

void sha_final(unsigned char *digest, struct sha_data *c) {
	size_t i = 0;

	#pragma unroll
	for (i = INITIAL_DATA_LEN+5; i < SHA_CHUNK_LEN-8; i++)
		c->data[i] = 0;

	sha_chunk(&(c->data), c);

	digest[ 0] = c->a >> 24;
	digest[ 1] = c->a >> 16;
	digest[ 2] = c->a >> 8;
	digest[ 3] = c->a;

	digest[ 4] = c->b >> 24;
	digest[ 5] = c->b >> 16;
	digest[ 6] = c->b >> 8;
	digest[ 7] = c->b;

	digest[ 8] = c->c >> 24;
	digest[ 9] = c->c >> 16;
}

__kernel void key_brute(
	__global unsigned int *results,
	__constant struct sha_data *partial,
	__constant unsigned char *search,
	const unsigned int raw_length,
	const unsigned int bitmask)
{
	unsigned int tx = get_global_id(0);
	unsigned int i,j;

	struct sha_data ctx;

	unsigned char digest[20];

	results[tx] = 0;

	/* Data area plus (useless) exponent area, and end bit */
	#pragma unroll
	for (j = 0; j < INITIAL_DATA_LEN+5; j++) {
		ctx.data[j] = partial->data[j];
	}

	#pragma unroll
	for (j = SHA_CHUNK_LEN - 8; j < SHA_CHUNK_LEN; j++) {
		ctx.data[j] = partial->data[j];
	}

	ctx.data[INITIAL_DATA_LEN] = tx >> 8;
	/* if MSB is 0, then it doesn't need to be stored in the key, so violates
	 * law of sizeof(e) == 4, messing everything up */
	if (ctx.data[INITIAL_DATA_LEN] == 0) {
		return;
	}
	ctx.data[INITIAL_DATA_LEN + 1] = tx;

	for (i = 3; i < 65536; i+=2) {
		ctx.a = partial->a;
		ctx.b = partial->b;
		ctx.c = partial->c;
		ctx.d = partial->d;
		ctx.e = partial->e;
//////////////////////////////////////////////////////////////
		ctx.data[INITIAL_DATA_LEN + 2] = i >> 8;
		ctx.data[INITIAL_DATA_LEN + 3] = i;
/////////////////////////////////////////////////////////////
		sha_final(&digest, &ctx);
		int all_clear;
		switch (raw_length) {
			case 10: /* nop */ break;
			case  9: all_clear =  (search[9] & bitmask) == (bitmask & (ctx.c >> 24)); break;
			case  8: all_clear =  (search[8] & bitmask) == (bitmask & (ctx.b	  )); break;
			case  7: all_clear =  (search[7] & bitmask) == (bitmask & (ctx.b >>  8)); break;
			case  6: all_clear =  (search[6] & bitmask) == (bitmask & (ctx.b >> 16)); break;
			case  5: /* nop */ break;
			case  4: all_clear =  (search[4] & bitmask) == (bitmask & (ctx.b >> 24)); break;
			case  3: all_clear =  (search[3] & bitmask) == (bitmask & (ctx.a	  )); break;
			case  2: all_clear =  (search[2] & bitmask) == (bitmask & (ctx.a >>  8)); break;
			case  1: all_clear =  (search[1] & bitmask) == (bitmask & (ctx.a >> 16)); break;
			case  0: all_clear =  (search[0] & bitmask) == (bitmask & (ctx.a >> 24)); break;

		}

		switch (raw_length) {
			case 10: all_clear &= (search[9] == ctx.c >> 16); /* fallthrough */
			case  9: all_clear &= (search[8] == ctx.c >> 24); /* fallthrough */
			case  8: all_clear &= (search[7] == ctx.b); /* fallthrough */
			case  7: all_clear &= (search[6] == ctx.b >> 8); /* fallthrough */
			case  6: all_clear &= (search[5] == ctx.b >> 16); /* fallthrough */
			case  5: all_clear &= (search[4] == ctx.b >> 24); /* fallthrough */
			case  4: all_clear &= (search[3] == ctx.a); /* fallthrough */
			case  3: all_clear &= (search[2] == ctx.a >> 8); /* fallthrough */
			case  2: all_clear &= (search[1] == ctx.a >> 16); /* fallthrough */
			case  1: all_clear &= (search[0] == ctx.a >> 24); /* fallthrough */
			case  0: /* nop */;
		}
		if (all_clear != 0) {
			results[tx] = i;
		}

//		int all_clear = 1;
//		for (j = 0; j < raw_length; j++) {
//			if (search[j] != digest[j]) {
//				all_clear = 0;
//			}
//		}
//		if (all_clear == 1 && (digest[j] & bitmask) == (search[j] & bitmask)) {
//			results[tx] = i;
//		}
	}

	return;
}