-
Notifications
You must be signed in to change notification settings - Fork 11
/
md5.cl
173 lines (152 loc) · 5.84 KB
/
md5.cl
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
// Copyright 2012 Alex Reece
/*
* This md5 kernel was heavily inspired by the md5 kernel in john the ripper
* community enhanced version. See https://github.com/magnumripper/JohnTheRipper
*
* Original software copyright (c) 2010, Dhiru Kholia
* <dhiru.kholia at gmail.com>,
* and it is hereby released to the general public under the following terms:
* Redistribution and use in source and binary forms, with or without modification,
* are permitted.
*
* Useful References:
* 1. CUDA MD5 Hashing Experiments, http://majuric.org/software/cudamd5/
* 2. oclcrack, http://sghctoma.extra.hu/index.php?p=entry&id=11
* 3. http://people.eku.edu/styere/Encrypt/JS-MD5.html
* 4. http://en.wikipedia.org/wiki/MD5#Algorithm
*/
#include "common.h"
#pragma OPENCL EXTENSION cl_khr_byte_addressable_store : enable
/* The basic MD5 functions */
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
/* The MD5 transformation for all four rounds. */
#define STEP(f, a, b, c, d, x, t, s) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b);
#define GET(i) (key[(i)])
// void md5_round(uint* internal_state, const uint* key);
static void md5_round(uint* internal_state, const uint* key) {
uint a, b, c, d;
a = internal_state[0];
b = internal_state[1];
c = internal_state[2];
d = internal_state[3];
/* Round 1 */
STEP(F, a, b, c, d, GET(0), 0xd76aa478, 7)
STEP(F, d, a, b, c, GET(1), 0xe8c7b756, 12)
STEP(F, c, d, a, b, GET(2), 0x242070db, 17)
STEP(F, b, c, d, a, GET(3), 0xc1bdceee, 22)
STEP(F, a, b, c, d, GET(4), 0xf57c0faf, 7)
STEP(F, d, a, b, c, GET(5), 0x4787c62a, 12)
STEP(F, c, d, a, b, GET(6), 0xa8304613, 17)
STEP(F, b, c, d, a, GET(7), 0xfd469501, 22)
STEP(F, a, b, c, d, GET(8), 0x698098d8, 7)
STEP(F, d, a, b, c, GET(9), 0x8b44f7af, 12)
STEP(F, c, d, a, b, GET(10), 0xffff5bb1, 17)
STEP(F, b, c, d, a, GET(11), 0x895cd7be, 22)
STEP(F, a, b, c, d, GET(12), 0x6b901122, 7)
STEP(F, d, a, b, c, GET(13), 0xfd987193, 12)
STEP(F, c, d, a, b, GET(14), 0xa679438e, 17)
STEP(F, b, c, d, a, GET(15), 0x49b40821, 22)
/* Round 2 */
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
/* Round 3 */
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
/* Round 4 */
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
internal_state[0] = a + internal_state[0];
internal_state[1] = b + internal_state[1];
internal_state[2] = c + internal_state[2];
internal_state[3] = d + internal_state[3];
}
void md5(const char* restrict msg, uint length_bytes, uint* restrict out) {
uint i;
uint bytes_left;
char key[64];
out[0] = 0x67452301;
out[1] = 0xefcdab89;
out[2] = 0x98badcfe;
out[3] = 0x10325476;
for (bytes_left = length_bytes; bytes_left >= 64;
bytes_left -= 64, msg = &msg[64]) {
md5_round(out, (const uint*) msg);
}
for (i = 0; i < bytes_left; i++) {
key[i] = msg[i];
}
key[bytes_left++] = 0x80;
if (bytes_left <= 56) {
for (i = bytes_left; i < 56; key[i++] = 0);
} else {
// If we have to pad enough to roll past this round.
for (i = bytes_left; i < 64; key[i++] = 0);
md5_round(out, (uint*) key);
for (i = 0; i < 56; key[i++] = 0);
}
ulong* len_ptr = (ulong*) &key[56];
*len_ptr = length_bytes * 8;
md5_round(out, (uint*) key);
}
__kernel void do_md5s(global const password_t* messages, global password_hash_t* out) {
int id = get_global_id(0);
uint i;
global password_hash_t* outhash = &out[id];
buffer_t message;
for (i = 0; i < messages[id].size_bytes; i++) {
message.buffer[i] = messages[id].password[i];
}
message.size = messages[id].size_bytes;
repeat_md5(&message);
for (i = 0; i < 4; i++) {
outhash->v[i] = ((uint*)message.buffer)[i];
}
}