-
Notifications
You must be signed in to change notification settings - Fork 5
/
oclvankus.py
executable file
·316 lines (203 loc) · 6.56 KB
/
oclvankus.py
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
#!/usr/bin/python3
# Oclvankus is a client/worker that computes chains.
#from __future__ import print_function
import pyopencl as cl
import numpy as np
import time, socket, os, sys, struct, threading
from libdeka import *
import libvankus
from vankusconf import mytables, HOST, PORT, kernels, slices
mf = cl.mem_flags
# just some context... You can define it with environment variable (you will be asked on first run)
ctx = cl.create_some_context()
#platform = cl.get_platforms()
#my_gpu_devices = platform[0].get_devices(device_type=cl.device_type.CPU)
#ctx = cl.Context(devices=my_gpu_devices)
cmdq = cl.CommandQueue(ctx)
# how many colors are there in each table
colors = 8
# length of one GSM burst
burstlen = 114
# length of one keystream sample
samplelen = 64
# samples in one burst (moving window)
samples = burstlen - samplelen + 1
# fragments in cl blob
clblobfrags = kernels * slices
# size of one fragment in longs (we need fragment + RF + challenge + flags)
onefrag = 4
# 64 ones
mask64 = 2**64-1
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
x = 0
# Reverse bits in integer
def revbits(x):
return int(bin(x)[2:].zfill(64)[::-1], 2)
# Background thread for network communication
def network_thr():
master_connect()
while True:
#print("Network!")
n = libvankus.getnumfree()
print("%i free slots"%n)
while libvankus.getnumfree() > 2:
if not get_startpoints():
break
while libvankus.getnumfree() > 2:
if not get_keystream():
break
put_work()
put_work()
put_cracked()
put_cracked()
time.sleep(0.2)
def master_connect():
global sock
sock.connect((HOST, PORT))
# Ask our master for keystream to crack
def get_keystream():
sock.sendall(bytes("getkeystream\r\n", "ascii"))
l = getline(sock)
jobnum = int(l.split()[0])
if jobnum == -1:
return False
keystream = l.split()[1]
part_add(jobnum, keystream)
return True
# Ask our master for startpoints to finish
def get_startpoints():
sock.sendall(bytes("getstart\r\n", "ascii"))
l = getline(sock)
jobnum = int(l.split()[0])
if jobnum == -1:
return False
keystream = l.split()[1]
plen = int(l.split()[2])
d = getdata(sock, plen)
print("Adding start")
complete_add(jobnum, keystream, d)
return True
# Post data package to our master
def put_result(command, jobnum, data):
sendascii(sock, "%s %i %i\r\n"%(command, jobnum, len(data)*8))
#print("len data %i"%len(data))
sendblob(sock, data)
# Post computed endpoints to our master
def put_dps(burst, n):
#print("reporting job %i len %i"%(n, len(burst)))
put_result("putdps", n, burst)
# Post cracked keys or finished jobs to our master
def put_cracked():
buf = np.zeros(100, dtype=np.uint8)
n = libvankus.pop_solution(buf)
if n >= 0:
s = buf.tostring()
pieces = s.split()
if pieces[0] == b'Found':
jobnum = int(pieces[4][1:])
sendascii(sock, "putkey %i %s\r\n"%(jobnum, toascii(s.rstrip(b'\x00'))))
if pieces[0] == b'crack':
jobnum = int(pieces[1][1:])
sendascii(sock, "finished %i\r\n"%jobnum)
# Add partial job (reconstructing the keyspace to the nearest endpoint)
def part_add(jobnum, bdata):
# add fragments to queue
bint = int(bdata,2)
fragbuf = np.zeros(9*colors*len(mytables)*samples, dtype=np.uint64)
ind = 0
# Generate keystream samples for all possible fragment combinations
for table in mytables:
for pos in range(0, samples):
for color in range(0, colors):
sample = (bint >> (samples-pos-1)) & mask64
#print("bint %X, sample %X, shift %i"%(bint, sample, (samples-pos-1)))
fragbuf[ind] = sample
fragbuf[ind+1] = jobnum
fragbuf[ind+2] = pos
fragbuf[ind+3] = 0
fragbuf[ind+4] = table
fragbuf[ind+5] = color
fragbuf[ind+6] = color
fragbuf[ind+7] = 8
fragbuf[ind+8] = 0
ind += 9
#print("Adding ",fragment)
#frags_q.put(fragment)
libvankus.burst_load(fragbuf)
# Add complete job (from the starting point towards the keystream sample)
def complete_add(jobnum, keystream, blob):
startpoints = struct.unpack("<%iQ"%(len(blob)/8), blob)
bint = int(keystream,2)
fragbuf = np.zeros(9*colors*len(mytables)*samples, dtype=np.uint64)
ind = 0
# Generate keystream samples for all possible combinations, however,
# using the sample as a challenge lookup and the starting point as PRNG input
for table in mytables:
for pos in range(0, samples):
for color in range(0, colors):
sample = (bint >> (samples-pos-1)) & mask64
fragbuf[ind] = startpoints[abs_idx(pos, table, color)]
fragbuf[ind+1] = jobnum
fragbuf[ind+2] = pos
fragbuf[ind+3] = 0
fragbuf[ind+4] = table
fragbuf[ind+5] = 0
fragbuf[ind+6] = 0
fragbuf[ind+7] = color+1
fragbuf[ind+8] = sample
ind += 9
libvankus.burst_load(fragbuf)
# Generate blob to be sent to OpenCL
def generate_clblob():
# Raw OpenCL buffer binary
clblob = np.zeros(clblobfrags * onefrag, dtype=np.uint64)
n = libvankus.frag_clblob(clblob)
return (n,clblob)
# Submit work to OpenCL & wait for results
def krak():
global x
(n,clblob) = generate_clblob()
if n == 0:
time.sleep(0.3)
return
# Generate device buffer
a = np.zeros(len(clblob), dtype=np.uint64)
s = np.uint32(a.shape)/4
# How many kernels to execute
kernelstoe = (n//(slices)+1,)
# Launch the kernel
print("Launching kernel, fragments %i, kernels %i"%(n,kernelstoe[0]))
print("Host lag %.3f s"%(time.time()-x))
x = time.time()
a_dev = cl.Buffer(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, hostbuf=clblob)
event = prg.krak(cmdq, kernelstoe, None, a_dev, s)
event.wait()
# copy the output from the context to the Python process
cl.enqueue_copy(cmdq, a, a_dev)
print("GPU computing: %.3f"%(time.time()-x))
x = time.time()
libvankus.report(a)
# Return absolute index of fragment in burst blob
def abs_idx(pos, table, color):
return mytables.index(table) * samples * colors + pos * colors + color
# Post finished work to our master
def put_work():
a = np.zeros(colors*len(mytables)*samples, dtype=np.uint64)
n = libvankus.pop_result(a)
if n >= 0:
put_dps(a, n)
# Start the net thread
net_thr = threading.Thread(target=network_thr, args=())
net_thr.start()
# Compile the kernel
FILE_NAME="slice.c"
f=open(FILE_NAME,"r")
SRC = ''.join(f.readlines())
f.close()
prg = cl.Program(ctx, SRC).build()
# Start processing
while (1):
if not net_thr.is_alive():
print("Network thread died :-(")
sys.exit(1)
krak()