-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
207 lines (160 loc) · 5.42 KB
/
server.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
from pbs import *
from pbs import Signer
from pbs import User
from fastecdsa.keys import gen_keypair, import_key
from fastecdsa.curve import P224
import json
import socket
import comm
from serialkey import serialkey
from base64 import b64decode
from Crypto.Hash import HMAC, SHA256
from typing import List, Tuple
from tqdm import tqdm
import sys
import debug
import multiprocessing as mp
import time
from itertools import repeat
HOST = "0.0.0.0"
PORT = 4444
_MSG = "hello"
_INFO = "world"
STATE = None
PRIV, _ = import_key("key.pri", P224, False)
#pointhashes = {}
#hints = {}
#This allows access from multiple processes
manager = mp.Manager()
pointhashes = manager.dict()
hints = manager.dict()
def parallelHash(i):
pointhashes[i],hints[i] = hashToPoint(str(i), P224, sha256)
def setup(numdays: int) -> socket.socket:
global pointhashes
global hints
print("Prehashing metadata to points...")
inputs = range(numdays)
pool = mp.Pool()
res = pool.map(parallelHash, inputs)
# for i in tqdm(range(numdays)):
# pointhashes[i], hints[i] = hashToPoint(str(i), P224, sha256)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((HOST, PORT))
server.listen(5)
return server
# def checkSerial(serial: str) -> bool:
# sbytes = b64decode(serial)
# s = sbytes[:16]
# mac = sbytes[16:]
# h = HMAC.new(serialkey, digestmod=SHA256)
# h.update(s)
# testmac = h.digest()
# if testmac == mac:
# return True
# return False
paramStates = manager.dict()
paramParam = manager.dict()
def parallelGenParams(i, pointhashes, s):
if i in pointhashes:
(state, param) = s.generate_params(str(i), pointhashes[i])
else:
(state, param) = s.generate_params(str(i))
paramStates[i] = state
paramParam[i] = param
def generate_params(s: Signer, msg: dict) -> Tuple[List[SignerState], List[SignatureParams]]:
global paramStates
global paramParam
num_params = msg["num_keys"]
#params = [] # type: list[SignatureParams]
#states = [] # type: list[SignerState]
print(f"Attempting to generate {num_params} parameters")
inputs = range(num_params)
pool = mp.Pool()
#def parallelGenParams(i, pointhashes, s):
pool.starmap(parallelGenParams, zip(inputs, repeat(pointhashes), repeat(s)))
# for i in tqdm(range(num_params)):
# if i in pointhashes:
# (state, param) = s.generate_params(str(i), pointhashes[i])
# else:
# (state, param) = s.generate_params(str(i))
# states.append(state)
# params.append(param)
states = list()
params = list()
for i in range(num_params):
states.append(paramStates[i])
params.append(paramParam[i])
return (states, params)
def sign_reqs(s: Signer, states: List[SignerState], msg) -> List[BlindedSignature]:
num_reqs = msg["num_keys"]
blinded_sigs = []
print(f"Attempting to sign {num_reqs} keys")
for i in tqdm(range(int(num_reqs))):
req = msg['req' + str(i)]
blinded_sig = s.sign(states[i], req)
blinded_sigs.append(blinded_sig)
return blinded_sigs
def handle_client(client: socket.socket):
if PRIV == None:
return
s = Signer(PRIV)
states = []
while True:
msg = comm.recvMessage(client)
#print("Received message:")
#print(msg)
msgType = msg['msg_type']
if msgType == comm.HELLO:
states, params = generate_params(s, msg)
data = {}
data['msg_type'] = comm.PARAMS
for i in range(len(params)):
data["params" + str(i)] = params[i]
if not i in pointhashes:
print("Error, asked for value outside of precomputed metadata. Maybe need to run server.py with a higher command line argument?")
data['msg_type'] = comm.ERROR
break
data["hint" + str(i)] = hints[i]
data['num_keys'] = str(len(params))
comm.sendMessage(client, data)
elif msgType == comm.SIGNREQ:
blinded_sigs = sign_reqs(s, states, msg)
data = {}
data['msg_type'] = comm.BLINDED
for i in range(len(blinded_sigs)):
data["blinded_sig" + str(i)] = blinded_sigs[i]
data['num_keys'] = str(len(blinded_sigs))
comm.sendMessage(client, data)
return True
elif msgType == comm.ERROR:
print(msg['error'])
return False
#def handle_hello(client: socket, msg: dict):
# # Check serial # with db
# if not checkSerial(msg['serial_number']):
# comm.sendError(client, "Serial does not exist in db")
# return False
# print(f"Serial: {msg['serial_number']} in db")
# return True
def handle_sig_request(msg: dict):
keys = msg['keys']
for i in range(len(keys)):
keys[i] = chr(ord(keys[i])+1)
return keys
def main():
numdays = 365
if len(sys.argv) == 2:
numdays = int(sys.argv[1])
server = setup(numdays)
print("Server up...")
while True:
client, addr = server.accept()
print(f"Received connection from {addr}")
if(handle_client(client)):
print("Sign Successful")
else:
print("Sign Unsuccessfull")
if __name__ == "__main__":
main()