-
Notifications
You must be signed in to change notification settings - Fork 1
/
timelock.py
467 lines (405 loc) · 14.7 KB
/
timelock.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python
DESCRIPTION = """
Theory:
Time-lock puzzles and timed-release Crypto (1996)
by Ronald L. Rivest, Adi Shamir, and David A. Wagner
Modified by Yuji Tomita 2015.
"""
import datetime
from Crypto.Util import number, randpool
from Crypto.Cipher import AES
import sys
import time
import argparse
# Init PyCrypto RNG
rnd = randpool.RandomPool()
# placeholder variable for packed files
if not 'puzzle' in locals():
puzzle = None
SECOND = 1
MINUTE = 60
HOUR = MINUTE * 60
DAY = HOUR * 24
MONTH = DAY * 31
YEAR = DAY * 365
MOD_BITS = 2048 # for time-lock puzzle N
AES_BITS = 192
def calibrate_speed():
p = number.getPrime(MOD_BITS/2, rnd.get_bytes)
q = number.getPrime(MOD_BITS/2, rnd.get_bytes)
N = p*q
bignum = number.getRandomNumber(MOD_BITS, rnd.get_bytes)
start = time.time()
trials = 100
for i in range(trials):
bignum = pow(bignum, 2, N)
return int(trials/(time.time() - start))
SPEED = calibrate_speed()
SAVE_INTERVAL = SPEED * 10 * MINUTE
def aes_pad(msg):
return msg + (16 - len(msg) % 16) * '\0'
def aes_encode(msg, key):
return AES.new(number.long_to_bytes(key)).encrypt(aes_pad(msg))
def aes_decode(ciphertext, key):
return AES.new(number.long_to_bytes(key)).decrypt(ciphertext)
# Routine adapted from Anti-Emulation-through-TimeLock-puzzles
def makepuzzle(t):
# Generate 512-bit primes
p = number.getPrime(MOD_BITS/2, rnd.get_bytes)
q = number.getPrime(MOD_BITS/2, rnd.get_bytes)
N = p*q
totient = (p-1)*(q-1)
key = number.getRandomNumber(AES_BITS, rnd.get_bytes)
a = number.getRandomNumber(MOD_BITS, rnd.get_bytes) % N
e = pow(2, t, totient)
b = pow(a, e, N)
cipher_key = (key + b) % N
return (key, {'N': N, 'a': a, 'steps': t, 'cipher_key': cipher_key})
def eta(remaining, speed):
seconds = remaining/speed
if seconds < 100 * SECOND:
return '%d seconds' % seconds
elif seconds < 100 * MINUTE:
return '%d minutes' % (seconds/MINUTE)
elif seconds < 100 * HOUR:
return '%d hours' % (seconds/HOUR)
elif seconds < 60 * DAY:
return '%d days' % (seconds/DAY)
elif seconds < 20 * MONTH:
return '%d months' % (seconds/MONTH)
else:
return '%d years' % (seconds/YEAR)
def putestimation(outputstream, puzzle):
outputstream.write("# Estimated time to solve: %s\n" % eta(puzzle['steps'], SPEED))
def save_puzzle(p):
state = str(p)
filename = 'puzzle_%d-%d' % (p['cipher_key'] % 1000000000000, p['steps']/SAVE_INTERVAL)
with open(filename, 'w') as f:
f.write('# Run ./timelock FILENAME > OUTFILE to decode\n')
putestimation(f, p)
f.write('\n')
f.write(state)
print >>sys.stderr, "saved state:", filename
def solve_puzzle(p):
tmp, N, t = p['a'], p['N'], p['steps']
start = time.time()
i = 0
while i < t:
if (i+1) % SAVE_INTERVAL == 0:
p2 = p.copy()
p2['steps'] = t-i
p2['a'] = tmp
save_puzzle(p2)
tmp = pow(tmp, 2, N)
if i % 12345 == 1:
speed = i/(time.time() - start)
sys.stderr.write('\r%f squares/s, %d remaining, eta %s \r'
% (speed, t-i, eta(t-i, speed)))
i += 1
print >>sys.stderr
return (p['cipher_key'] - tmp) % N
def _unpack():
solution = solve_puzzle(puzzle)
print >>sys.stderr, "solution =", solution
if 'ciphertext' in puzzle:
result = aes_decode(puzzle['ciphertext'], solution)
print result
with open('decoded.txt', 'a') as f:
f.write(result + "\n")
def _usage():
if puzzle:
print """*** This is a self-decoding file ***
If no parameter is given, the embedded puzzle will be decoded.
"""
print """Usage: ./timelock.py <PARAM>
--h|help display this message
--new [time] create a sample puzzle with solution time 'time'
--encrypt <file> [time] encode a file using AES with a random key
--pack <file> [time] pack a self-decoding file using this script
--benchmark print number of operations per second
<saved state> print puzzle solution to stdout
"""
exit(2)
def _new_key_time0(time):
try:
time = int(sys.argv[2]) * SECOND
except:
time = 30 * SECOND
print "Creating test puzzle with difficulty time %d" % time
(key, puzzle) = makepuzzle(time*SPEED)
print "key:", str(key) # Recover the key
save_puzzle(puzzle)
def _encrypt_file_time0(file, time, value=None):
if value is not None:
msg = value
else:
msg = open(file).read()
try:
time = int(time)
except:
try:
time = int(sys.argv[3]) * SECOND
except:
time = 30 * SECOND
(key, puzzle) = makepuzzle(time*SPEED)
puzzle['ciphertext'] = aes_encode(msg, key)
save_puzzle(puzzle)
def _pack_file_time0(self, file, time, value=None, save_to_file=None):
if save_to_file is not None:
import StringIO
stdout = sys.stdout
sys.stdout = StringIO.StringIO()
if value is not None:
msg = value
else:
msg = open(file).read()
try:
time = int(time)
except:
try:
time = int(sys.argv[3]) * SECOND
except:
time = 30 * SECOND
(key, puzzle) = makepuzzle(time*SPEED)
puzzle['ciphertext'] = aes_encode(msg, key)
print "#!/usr/bin/env python"
for line in DESCRIPTION.split('\n'):
print "#", line
print "# Run this program to recover the original message."
print "# (scroll down see the program that generated this file)"
print "#"
putestimation(sys.stdout, puzzle)
print "#"
print
print "puzzle =", puzzle
print open(self).read()
if save_to_file is not None:
with open(save_to_file, 'w') as f:
f.write(sys.stdout.getvalue())
sys.stdout = stdout
def _decode_file(file):
try:
print "Decoding %s" % file
puzzle = eval(open(file).read())
except Exception, e:
print "Error parsing saved state.", e
exit(1)
solution = solve_puzzle(puzzle)
print >>sys.stderr, "solution =", solution
if 'ciphertext' in puzzle:
print aes_decode(puzzle['ciphertext'], solution)
# class ArgList(list):
# def __init__(self, *args):
# list.__init__(self, *args)
# self.base = self[0]
# self.first = self[1]
# self.second = self[2]
# self.third = self[3]
# def __getitem__(self, i):
# if i >= len(self):
# return None
# return list.__getitem__(self, i)
# def main():
# args = ArgList(sys.argv)
# if args.first == '-h' or args.first == '--help':
# _usage()
# elif len(args) == 1 and puzzle:
# _unpack()
# elif len(args) == 1:
# _usage()
# elif args.first == '--new':
# _new_key_time0(args.second)
# elif args.first == '--benchmark':
# print "%d %d-bit modular exponentiations per second" % (SPEED, MOD_BITS)
# elif args.first == '--encrypt':
# _encrypt_file_time0(args.second, args.third)
# elif args[1] == '--pack':
# _pack_file_time0(args.base, args.second, args.third)
# else:
# _decode_file(args.first)
# print """Usage: ./timelock.py <PARAM>
# --h|help display this message
# --new [time] create a sample puzzle with solution time 'time'
# --encrypt <file> [time] encode a file using AES with a random key
# --pack <file> [time] pack a self-decoding file using this script
# --benchmark print number of operations per second
# <saved state> print puzzle solution to stdout
class Main(object):
def __init__(self, args):
self.args = args
print >> sys.stderr, args
def execute(self):
if self.args.benchmark:
self.benchmark()
elif self.args.pack:
self.pack()
elif self.args.seconds_until_date or self.args.until_date:
self.seconds_until_date(self.args.seconds_until_date or self.args.until_date)
elif self.args.encrypt:
self.encrypt()
elif self.args.decode:
_decode_file(self.args.decode)
else:
sys.exit(1)
def exit(self, msg=''):
print >> sys.stderr, u"Exit: %s" % msg
sys.exit(1)
def encrypt(self):
""" Encrypt a file now.
"""
_encrypt_file_time0(
None,
self.get_time_to_decode_seconds(),
value=self.get_value_to_encode(),
)
def pack(self, seconds=None, save_to_file=None):
""" Pack a self encoding file to stdout.
"""
self_file = sys.argv[0]
print >> sys.stderr, "Packing... ", seconds, save_to_file
_pack_file_time0(
self_file,
None,
seconds or self.get_time_to_decode_seconds(),
value=self.get_value_to_encode(),
save_to_file=save_to_file,
)
def get_time_to_decode_seconds(self):
""" Get the time the user wishes to take to decode this file.
"""
seconds = self.get_unit() * self.args.time
print >> sys.stderr, "Calculated seconds to: %s" % seconds
return seconds
def convert_date_to_seconds(self, date_string):
""" Process a date string to seconds.
"""
pass
def get_unit(self):
UNIT_MAP = {
'seconds': SECOND,
'minutes': MINUTE,
'hours': HOUR,
'days': DAY,
'years': YEAR,
'date': self.convert_date_to_seconds,
}
unit = self.args.unit
seconds_per_value = UNIT_MAP[unit]
return seconds_per_value
def seconds_until_date(self, arg):
""" Calculate seconds until a date.
Common Time Zones for USA.
'US/Alaska',
'US/Arizona',
'US/Central',
'US/Eastern',
'US/Hawaii',
'US/Mountain',
'US/Pacific',
'UTC',
"""
print >> sys.stderr,'Calculating time until a date %s' % arg
try:
from dateutil import parser
import pytz
except ImportError:
self.exit("You need to install python-dateutil and pytz to use the date functionality")
COMMON_TZINFOS = {
'PDT': pytz.timezone('US/Pacific'),
'PST': pytz.timezone('US/Pacific'),
'EST': pytz.timezone('US/Eastern'),
'EDT': pytz.timezone('US/Eastern'),
'CST': pytz.timezone('US/Central'),
}
date_format = '%m-%d-%Y %H:%M %Z'
server_utc = datetime.datetime.now(pytz.utc)
print >> sys.stderr, "Server Now UTC: ", server_utc
server_est = server_utc.astimezone(pytz.timezone('US/Eastern'))
print >> sys.stderr, "Server Now EST: ", server_est
if len(arg) > 1:
arg = u' '.join(arg)
else:
arg = arg[0]
target_date = parser.parse(arg)
if self.args.tz:
try:
tz = COMMON_TZINFOS[self.args.tz]
except KeyError:
tz = pytz.timezone(self.args.tz)
target_date_tz = tz.localize(target_date)
target_date_utc = target_date_tz.astimezone(pytz.utc)
else:
proceed = raw_input("No TZ passed. Assuming EST.\nProceed?\ny/n: ")
if proceed != 'y':
self.exit("Exited")
tz = pytz.timezone('US/Eastern')
target_date_tz = tz.localize(target_date)
target_date_utc = target_date_tz.astimezone(pytz.utc)
target_date_string = target_date_tz.strftime('%A, %B %Y at %I%p %Z').strip()
print >> sys.stderr, "Target Date UTC: ", target_date_utc
print >> sys.stderr, "Target Date %s: " % tz, target_date_string
delta = target_date_tz - server_utc
seconds = delta.total_seconds()
default_filename = ''.join((
'TARGET__',
target_date_string.replace(' ', '_').replace(',', ''),
('-%.0fHRS' % (seconds/HOUR)),
'.py'
))
print >> sys.stderr, """Time difference is {delta}.
Seconds: {seconds:.0f}
Minutes: {minutes:.0f}
Hours: {hours:.1f}
Days: {days:.2f}
Target Unlock Date: {target_date}
""".format(
delta=delta,
seconds=seconds,
minutes=seconds/MINUTE,
hours=seconds/HOUR,
days=seconds/DAY,
target_date=target_date_string
)
filename = raw_input("Enter the output file name\nLeave blank to default name:\n%s\n" % default_filename)
self.pack(seconds, save_to_file=filename or default_filename)
def get_value_to_encode(self):
if self.args.file:
with open(self.args.file) as f:
return f.read()
elif self.args.value:
return self.args.value
self.exit("No string or file provided to encode")
def benchmark(self):
print "%d %d-bit modular exponentiations per second" % (SPEED, MOD_BITS)
def main():
if puzzle:
print >> sys.stderr, 'this is a self decoding file - decoding.'
_unpack()
return
parser = argparse.ArgumentParser()
parser.add_argument('value', nargs='?', help="Provide a string to encrypt.")
parser.add_argument('-b', '--benchmark', help="Print number of operations per second", required=False, action="store_true")
parser.add_argument('-p', '--pack', help="Pack a self decoding python file given a file", required=False, action="store_true")
parser.add_argument('-f', '--file', help="Provide a file to encrypt.", required=False)
parser.add_argument('-t', '--time', help="Time to decode", required=False, type=int)
parser.add_argument('-e', '--encrypt', help="Encrypt a file that can be unencrypted in X seconds", required=False, action="store_true")
parser.add_argument('-d', '--decode', help="Encrypt a file that can be unencrypted in X seconds", required=False)
parser.add_argument('-u', '--unit', help="Time unit to use when interpreting time input", required=False, default='seconds', choices=[
'seconds',
'minutes',
'hours',
'days',
'months',
'years',
])
parser.add_argument('-U', '--until-date', nargs="+", help="Encode until a date", required=False)
parser.add_argument('--tz', help="Provide a Time Zone. PST/EST or all of the full codes such as US/Eastern", required=False)
parser.add_argument('--seconds-until-date', nargs="+", help="Get seconds until a date", required=False)
# show help if no args
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
Main(args).execute()
if __name__ == "__main__":
main()