-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
215 lines (169 loc) · 6.06 KB
/
client.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
from pbs import *
from pbs import Signer
from pbs import User
from fastecdsa.keys import gen_keypair, import_key
from fastecdsa.curve import P224
from hashlib import sha256
import json
import socket
import comm
import sys
from typing import List, Tuple
from tqdm import tqdm
import debug
import multiprocessing as mp
import pdb
from itertools import repeat
HOST = "localhost"
PORT = 4444
_, PUB = import_key("key.pub", P224, True)
manager = mp.Manager()
_states = manager.dict()
_reqs = manager.dict()
_pubkeys = dict()
_u = dict()
_params = dict()
_hints = dict()
def gen_keys(privateseed: str, numkeys: int) -> List[str]:
print(f"Generating {numkeys} keys")
pubkeys = []
for i in tqdm(range(numkeys)):
pkey = hashToInt(privateseed + str(i), P224, sha256)
pubkey = pkey * P224.G
pubkeyx = '{:056x}'.format(pubkey.x)
pubkeys.append(pubkeyx)
return pubkeys
unblindSigs = manager.dict()
def parallelUnblind(i, pubkeys, u, states, hints, msg):
global unblindSigs
bsig = msg["blinded_sig" + str(i)]
keyhash = sha256(pubkeys[i].encode()).hexdigest()
sig = u.unblind(states[i], bsig, keyhash, str(i),
hint=hints[i], aux=pubkeys[i])
unblindSigs[i] = sig
def unblindKeys(u: User, states: List[UserState], msg: dict, pubkeys: List[str], hints: List[int]) -> List[UnblindedSignature]:
global unblindSigs
#sigs = []
num_keys = msg["num_keys"]
print(f"Attempting to unblind {num_keys} keys with {len(states)} states")
inputs = range(int(num_keys))
pool = mp.Pool()
pool.starmap(parallelUnblind, zip(inputs, repeat(pubkeys), repeat(u), repeat(states), repeat(hints), repeat(msg)))
# for i in tqdm(range(int(num_keys))):
# bsig = msg["blinded_sig" + str(i)]
# keyhash = sha256(pubkeys[i].encode()).hexdigest()
# sig = u.unblind(states[i], bsig, keyhash, str(i),
# hint=hints[i], aux=pubkeys[i])
# sigs.append(sig)
# pdb.set_trace()
retVals = list()
for i in range(int(num_keys)):
retVals.append(unblindSigs[i])
return retVals
def get_params(msg: dict) -> Tuple[List[SignatureParams], List[int]]:
params = []
num_reqs = msg["num_keys"]
hints = []
print(f"Attempting to decode {num_reqs} params from server")
for i in range(int(num_reqs)):
params.append(msg["params" + str(i)])
hints.append(msg["hint" + str(i)])
return params, hints
def parallelReq(i):
global _states
global _reqs
keyhash = sha256(_pubkeys[i].encode()).hexdigest()
state, req = _u.generate_signature_request(
_params[i], keyhash, str(i), hint=_hints[i])
_states[i] = state
_reqs[i] = req
def gen_requests(u: User, params: List[SignatureParams], pubkeys: List[str], hints: List[int]) -> Tuple[List[UserState], List[int]]:
print(
f"Generating {len(pubkeys)} signing requests from {len(params)} server params")
global _states
global _reqs
global _pubkeys
global _u
global _params
global _hints
_pubkeys = pubkeys
_u = u
_params = params
_hints = hints
inputs = range(len(pubkeys))
pool = mp.Pool()
res = pool.map(parallelReq, inputs)
# states = list()
# reqs = list()
# for i in tqdm(range(len(pubkeys))):
# keyhash = sha256(pubkeys[i].encode()).hexdigest()
# state, req = u.generate_signature_request(
# params[i], keyhash, str(i), hint=hints[i])
# states.append(state)
# reqs.append(req)
# pdb.set_trace()
states_retval = list()
reqs_retval = list()
for i in range(len(pubkeys)):
states_retval.append(_states[i])
reqs_retval.append(_reqs[i])
return (states_retval, reqs_retval)
# return (_states.values(), _reqs.values())
def handle_server(conn: socket.socket, serial_number: str, numkeys: int, privateseed: str) -> None:
# generate connection request with serial number as datafield and send request
print("Sending hello")
req = {"msg_type": comm.HELLO,
"serial_number": serial_number, "num_keys": numkeys}
comm.sendMessage(conn, req)
print("Waiting for server response")
u = User(PUB)
states = []
pubkeys = []
hints = []
while True:
msg = comm.recvMessage(conn)
#print("Received message:")
# print(msg)
msgType = msg['msg_type']
if msgType == comm.PARAMS:
params, hints = get_params(msg)
pubkeys = gen_keys(privateseed, numkeys)
(states, reqs) = gen_requests(u, params, pubkeys, hints)
data = {}
data['msg_type'] = comm.SIGNREQ
data['num_keys'] = len(pubkeys)
for i in range(len(pubkeys)):
data['req' + str(i)] = reqs[i]
print(f"Sending requests")
comm.sendMessage(conn, data)
elif msgType == comm.BLINDED:
sigs = unblindKeys(u, states, msg, pubkeys, hints)
print("Writing keys to file")
with open("signed_keys.txt", "w") as out:
for s in sigs:
out.write(json.dumps(s, cls=ECEncoder))
out.write("\n")
print("Done")
sys.exit(0)
elif msgType == comm.ERROR:
print(msg['error'])
return
def main():
if len(sys.argv) < 3:
print("Usage: python3 client.py <serial> <privateseed> <number of keys (optional)>")
print("Serial can be obtained from serialgen.py")
print("Private seed is a string used as input to a PRG to generate private keys corresponding to the public keys that are signed in this protocol")
print("Number of keys specifies how many keys will be generated and signed in this execution")
sys.exit(0)
serial = sys.argv[1]
privateseed = sys.argv[2]
if len(sys.argv) == 4:
numkeys = int(sys.argv[3])
else:
numkeys = 365
# type: socket.socket
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((HOST, PORT))
handle_server(conn, serial, numkeys, privateseed)
if __name__ == "__main__":
main()