-
Notifications
You must be signed in to change notification settings - Fork 9
/
dut-openssl.py
executable file
·97 lines (76 loc) · 2.49 KB
/
dut-openssl.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
#!/usr/bin/env python
import socket
import time
import subprocess
from random import getrandbits
from config import config_get, config_reload
from config import *
def n2hex(n, length=512):
res = ""
while n > 0:
res = "%02x" % (n%256) + res
n /= 256
res = ("0"*(length - len(res))) + res
return res
# list of allowed programs and some test values
PROGS = { "./cprog/openssl-mul-4096": n2hex(getrandbits(4095)),
"./cprog/openssl-mul": n2hex(getrandbits(2047)),
"./cprog/openssl-mul-mont": n2hex(getrandbits(2047)),
"./cprog/openssl-exp": n2hex(getrandbits(2047)),
"./cprog/openssl-exp-4096": n2hex(getrandbits(4095)),
"./cprog/openssl-exp-bin": n2hex(getrandbits(2047)),
"./cprog/openssl-exp-bin-4096": n2hex(getrandbits(4095)) }
# DUT client
class dut():
def __init__(self):
self.apply_config()
self.connect()
def apply_config(self):
config_reload()
self.cmd = config_get("misc.cmd", str)
self.ip = config_get("misc.ip", str)
self.port = config_get("misc.port", int)
self.test_value = n2hex(2**2048-1)
def connect(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.ip, self.port))
self.s = s
def reconnect(self):
self.s.close()
self.connect()
def challenge(self, challenge):
self.s.send("%s %s" % (self.cmd, str(challenge)))
response = self.s.recv(1024)
# DUT daemon
class dut_service():
def __init__(self):
self.apply_config()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("0.0.0.0", self.port))
s.listen(1)
s.setblocking(1)
self.s = s
def apply_config(self):
config_reload()
self.port = config_get("misc.port", int)
def run(self):
while True:
conn, addr = self.s.accept()
try:
while True:
request = conn.recv(4096)
if not request: break
response = self.work(request)
conn.send(response)
conn.close()
except socket.error:
pass
def work(self, challenge):
cmd = challenge.split(" ")
if cmd[0] in VALUES:
subprocess.call(cmd)
return "ok"
if __name__ == "__main__":
d = dut_service()
d.run()