-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·359 lines (310 loc) · 13.1 KB
/
main.cpp
File metadata and controls
executable file
·359 lines (310 loc) · 13.1 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma comment(lib, "OpenCL.lib")
#pragma comment(lib, "Ws2_32.lib")
#endif
#include "sha256.h"
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <chrono>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <stdint.h>
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
uint64_t microseconds() {
LARGE_INTEGER fq, t;
QueryPerformanceFrequency(&fq);
QueryPerformanceCounter(&t);
return (1000000 * t.QuadPart) / fq.QuadPart;
}
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#endif
const int nloops = 256; // the same number needs to be set in the kernel!
// https://stackoverflow.com/a/23898449/266720
uint8_t tallymarker_hextobin(const char * str, char * bytes, size_t blen) {
uint8_t pos;
uint8_t idx0;
uint8_t idx1;
// mapping of ASCII characters to hex values
const uint8_t hashmap[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // !"#$%&'
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ()*+,-./
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, // 01234567
0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 89:;<=>?
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // @ABCDEFG
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // HIJKLMNO
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // PQRSTUVW
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // XYZ[\]^_
0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, // `abcdefg
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // hijklmno
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pqrstuvw
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // xyz{|}~.
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
};
bzero(bytes, blen);
for (pos = 0; ((pos < (blen*2)) && (pos < strlen(str))); pos += 2) {
idx0 = (uint8_t)str[pos+0];
idx1 = (uint8_t)str[pos+1];
bytes[pos/2] = (uint8_t)(hashmap[idx0] << 4) | hashmap[idx1];
};
return(0);
}
int main(int argc, char *argv[]) {
printf("CLTUNA OpenCL core v1.4\n");
printf("usage: ./cltuna [hostname] [port] [platform] [device]\n");
printf("examples: ./cltuna (default)\n");
printf(" ./cltuna 0.0.0.0 12345 (run exposed to network on port 12345)\n");
printf(" ./cltuna benchmark (run benchmark)\n");
printf(" ./cltuna list (show list of platform and device IDs)\n");
char hostname[256] = "127.0.0.1"; // only accessible on the same machine, use 0.0.0.0 to make it available to the network
uint16_t port = 2023;
int platform_idx = 0;
int device_idx = 0;
if (argc > 1) {
strncpy(hostname, argv[1], 255);
hostname[255] = 0;
}
if (argc > 2) {
port = atoi(argv[2]);
}
if (argc > 3) {
platform_idx = atoi(argv[3]);
}
if (argc > 4) {
device_idx = atoi(argv[4]);
}
// result string which will be returned
char result[256];
// input plaintext
char bytes[68];
// list all devices
if (argc > 1 && strncmp(hostname, "list", 4) == 0) {
listDevices();
exit(0);
}
sha256_init(platform_idx, device_idx);
srand(time(NULL));
// run with any argument to start benchmark mode
if (argc > 1 && strncmp(hostname, "benchmark", 9) == 0) {
auto begin = std::chrono::high_resolution_clock::now();
uint64_t n_hashes = 0;
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - begin).count() < 1100) {
memset(result, 0, 256);
memset(bytes,0,67);
sha256_crypt(bytes, 67, 2, 6, 1, result);
n_hashes += npar*nloops;
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
double hash_rate = (double)(n_hashes)/(elapsed.count() * 0.001);
printf("hash rate: %f/s\n", hash_rate);
exit(0);
} else if (argc > 1 && strncmp(hostname, "find", 4) == 0) {
if (argc > 6) {
int LZ = atoi(argv[5]);
int DN = atoi(argv[6]);
int num = 1;
if (argc > 7) {
num = atoi(argv[7]);
}
int num_total = num;
printf("find %d hashes with LZ=%d DN=%d...\n", num_total, LZ, DN);
auto begin = std::chrono::high_resolution_clock::now();
while (num > 0) {
memset(result, 0, 256);
memset(bytes,0,67);
sha256_crypt(bytes, 67, 0, LZ, DN, result);
if (result[0] != 0) {
printf("\x1b[32mfound: %s\x1b[0m\n", result);
num -= 1;
}
}
auto end = std::chrono::high_resolution_clock::now();
printf("found %d hashes with LZ=%d DN=%d in %lu seconds.\n", num_total, LZ, DN, std::chrono::duration_cast<std::chrono::seconds>(end - begin).count());
exit(0);
} else {
printf("find needs at least 6 arguments.\n");
exit(7);
}
}
printf("starting...\n");
#ifdef _WIN32
int iResult;
WSADATA wsaData;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
#endif
// Socket communication
printf("Creating socket...\n");
int socket_desc, client_sock;
socklen_t client_size;
struct sockaddr_in server_addr, client_addr;
char server_message[2000], client_message[2000], tmp_message[2000];
memset(server_message, '\0', sizeof(server_message));
memset(client_message, '\0', sizeof(client_message));
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if(socket_desc < 0){
printf("Error while creating socket\n");
return -1;
}
printf("Socket created successfully\n");
// Set port and IP:
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(hostname);
// Bind to the set port and IP:
if(bind(socket_desc, (struct sockaddr*)&server_addr, sizeof(server_addr))<0){
printf("Couldn't bind to the port\n");
return -1;
}
printf("Done with binding\n");
// Run a single test of SHA256
memset(result,0,256);
memset(bytes,0,68);
sha256_crypt(bytes, 67, 2, 8, 65532, result);
printf("TEST: %s\n", result);
printf("--------\n");
// Wait for work
while(true) {
// Listen for clients
if(listen(socket_desc, 1) < 0){
printf("Error while listening\n");
return -1;
}
printf("\nListening for incoming connections on %s:%d.....\n", hostname, port);
// Accept an incoming connection
client_size = sizeof(client_addr);
client_sock = accept(socket_desc, (struct sockaddr*)&client_addr, &client_size);
if (client_sock < 0){
printf("Can't accept\n");
break;
}
printf("Client connected at IP: %s and port: %i\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
while(true) {
auto time_0 = std::chrono::high_resolution_clock::now();
// Receive client's message
memset(client_message, '\0', sizeof(client_message));
memset(tmp_message, '\0', sizeof(tmp_message));
while( strlen(tmp_message) < 134) {
memset(client_message, '\0', sizeof(client_message));
memset(tmp_message, '\0', sizeof(tmp_message));
if (recv(client_sock, tmp_message, sizeof(tmp_message), 0) < 0){
printf("Couldn't receive\n");
break;
}
if (strlen(tmp_message) < 134) {
printf("\x1b[31merror: invalid message.\x1b[0m\n");
exit(8);
}
}
// Delimited by \n, only take last message and discard all previous ones
char *token;
const char s[2] = "\n";
token = strtok(tmp_message, s);
while( token != NULL && strlen(token) > 2) {
strncpy(client_message, token, sizeof(client_message));
client_message[strlen(token)] = 0;
token = strtok(NULL, s);
}
printf("Msg from client [%lu]: %s \n", strlen(client_message), client_message);
char * txbytes_hex = strtok(client_message, " "); // DATUM
char * token2 = strtok(NULL, " "); // LZ
char * token3 = strtok(NULL, " "); // DN
int LZ = atoi(token2);
int DN = atoi(token3);
printf("TX: %s, LZ: %d, DN: %d\n", txbytes_hex, LZ, DN);
// HEX to bytes
tallymarker_hextobin(txbytes_hex, bytes, 67);
bytes[67]=0;
#ifdef _WIN32
srand( microseconds() + rand());
#else
struct timeval tv;
gettimeofday(&tv,NULL);
unsigned long time_in_micros = 1000000 * tv.tv_sec + tv.tv_usec;
srand(time_in_micros + rand());
#endif
auto time_1 = std::chrono::high_resolution_clock::now();
uint64_t n_hashes = 0;
bool found = false;
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - time_1).count() < 3100) {
memset(result, 0, 256);
// Do the magic and get some fish!
sha256_crypt(bytes, 67, 0, LZ, DN, result);
n_hashes += npar*nloops;
// If hash has been found
if (result[0] != 0) {
printf("\n\n\x1b[32mfound: %s\x1b[0m\n\n\n", result);
strcpy(server_message, result);
// Send message back to client
if (send(client_sock, server_message, strlen(server_message), 0) < 0){
printf("Can't send\n");
break;
}
found = true;
break;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - time_0);
double hash_rate = (double)(n_hashes)/(elapsed.count() * 0.001);
uint64_t seconds = (pow(2,4*LZ)*65536./(1+DN)) / hash_rate;
uint64_t weeks = seconds / (86400*7);
seconds -= weeks * 86400*7;
uint64_t days = seconds / 86400;
seconds -= days * 86400;
uint64_t hours = seconds / 3600;
seconds -= hours * 3600;
uint64_t minutes = seconds / 60;
seconds -= minutes*60;
if (weeks > 4) {
printf("hash rate: %f, expect block every %ld weeks %ld days. \n", hash_rate, weeks, days);
} else if (weeks > 0 || days > 0) {
printf("hash rate: %f, expect block every %ld days %ld hours. \n", hash_rate, weeks*7+days, hours);
} else if (hours > 0) {
printf("hash rate: %f, expect block every %ld hours %ld minutes. \n", hash_rate, hours, minutes);
} else {
printf("hash rate: %f, expect block every %ld minutes %ld seconds. \n", hash_rate, minutes, seconds);
}
// Respond to client with ping (single "." char to signal alive and ready)
strcpy(server_message, ".");
if (!found) {
if (send(client_sock, server_message, strlen(server_message), 0) < 0){
printf("Can't send\n");
break;
}
}
printf("done, waiting for client...\n");
}
}
}