-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
337 lines (259 loc) · 11.7 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
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
from flask import Flask, jsonify, request, abort
from umbral import config, pre, keys, signing
from umbral.curve import SECP256K1
from flask_cors import CORS
import random
import json
from umbral.pre import Capsule
from umbral.kfrags import KFrag
config.set_default_curve(SECP256K1)
app = Flask(__name__)
CORS(app)
from umbral.config import default_params
params = default_params()
# params
def getUmbralPublicFromHex(str):
public_key_dirty = str
public_key_hex = "03" + public_key_dirty[-64:]
public_key = keys.UmbralPublicKey.from_bytes(bytes.fromhex(public_key_hex))
return public_key
def getUmbralPrivateFromHex(str):
private_key_dirty = str
private_key_hex = private_key_dirty[-64:]
private_key = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(private_key_hex))
return private_key
@app.route('/setup', methods=['POST'])
def setup_route():
if not request.json :
abort(400)
# '0x16387146e0be3b7da18fc40e3c231c4d320d0e087c310ac49de1c178dde54398'
print(request.json)
alice_dirty_private_key = request.json['alices_private_key']
print(request.json)
alices_private_key_hex = alice_dirty_private_key[-64:]
alices_private_key = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(alices_private_key_hex))
# bobs_private_key = keys.UmbralPrivateKey.gen_key()
# "0xb6bb217955486f4cd9b2662fa909b0d4a207c8dae6f148be5e7ca94ed3fbdf0c"
bobs_dirty_private_key = request.json['bobs_private_key']
bobs_private_key_hex = bobs_dirty_private_key[-64:]
bobs_private_key = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(bobs_private_key_hex))
# n = int(request.json['n'] || '3')
n = int(request.json['n'])
#m = int(request.json['m'] || '2')
m = int(request.json['m'])
alices_public_key, alices_signing_key, alices_verifying_key, bobs_public_key, kfrags = setup(alices_private_key, bobs_private_key, n, m)
# print(result)
serialized_kfrags = list()
for kfrag in kfrags:
serialized_kfrags.append(kfrag.to_bytes().hex())
responce = json.dumps({
"alices_public_key": alices_public_key.to_bytes().hex(),
"alices_signing_key": alices_signing_key.to_bytes().hex(),
"alices_verifying_key": alices_verifying_key.to_bytes().hex(),
"kfrags": serialized_kfrags
})
print(responce)
return responce, 200
def setup(alices_private_key, bobs_private_key, n, m):
#################
# Generate Umbral keys for Alice.
################# []
# alices_private_key = keys.UmbralPrivateKey.gen_key()
alices_public_key = alices_private_key.get_pubkey()
print("alices_private_key", alices_private_key)
alices_signing_key = keys.UmbralPrivateKey.gen_key()
alices_verifying_key = alices_signing_key.get_pubkey()
# assert alices_public_key == alices_verifying_key # it souldn't
alices_signer = signing.Signer(private_key=alices_signing_key)
# Generate Umbral keys for Bob.
################# []
# bobs_private_key = keys.UmbralPrivateKey.gen_key()
bobs_public_key = bobs_private_key.get_pubkey()
print("bobs_public_key", bobs_public_key)
# Alice generates "M of N" re-encryption key fragments (or "KFrags") for Bob.
# In this example, 10 out of 20.
kfrags = pre.generate_kfrags(delegating_privkey=alices_private_key,
signer=alices_signer,
receiving_pubkey=bobs_public_key,
threshold=m,
N=n)
return alices_public_key, alices_signing_key, alices_verifying_key, bobs_public_key, kfrags
@app.route('/en', methods=['POST'])
def en_route():
if not request.json :
abort(400)
alices_public_key_dirty = request.json['alices_public_key']
alices_public_key_hex = "03" + alices_public_key_dirty[-64:]
alices_public_key = keys.UmbralPublicKey.from_bytes(bytes.fromhex(alices_public_key_hex))
plaintext = request.json['plaintext']
ciphertext, capsule = en(alices_public_key, plaintext)
print (ciphertext)
return json.dumps({
"ciphertext":ciphertext.hex(),
"capsule": capsule.to_bytes().hex()
}), 200
def en(alices_public_key, plaintext):
################# ENCRYPT
# Encrypt data with Alice's public key.
################# []
# plaintext = b'Proxy Re-Encryption is cool!'
ciphertext, capsule = pre.encrypt(alices_public_key, str.encode(plaintext))
# print("params", params.to_bytes())
return ciphertext, capsule
@app.route('/de', methods=['POST'])
def de_route():
if not request.json:
abort(400)
capsule_raw = request.json['capsule']
capsule = Capsule.from_bytes(bytes.fromhex(capsule_raw), params)
alices_public_key = getUmbralPublicFromHex(request.json['alices_public_key'])
alices_verifying_key = getUmbralPublicFromHex(request.json['alices_verifying_key']) # ? maybe without 03
ciphertext = bytes.fromhex(request.json['ciphertext'])
bobs_private_key = getUmbralPrivateFromHex(request.json['bobs_private_key'])
# kfrags
serialized_kfrags = request.json['kfrags']
kfrags = []
for skfrag in serialized_kfrags:
kfrags.append(KFrag.from_bytes(bytes.fromhex(skfrag)))
responce = de(capsule, alices_public_key, alices_verifying_key, ciphertext, bobs_private_key, kfrags)
print(responce)
return responce, 200
def de(capsule, alices_public_key, alices_verifying_key, ciphertext, bobs_private_key, kfrags):
bobs_public_key = bobs_private_key.get_pubkey()
# Several Ursulas perform re-encryption, and Bob collects the resulting `cfrags`.
# He must gather at least `threshold` `cfrags` in order to activate the capsule.
capsule.set_correctness_keys(delegating=alices_public_key,
receiving=bobs_public_key,
verifying=alices_verifying_key)
cfrags = list() # Bob's cfrag collection
for kfrag in kfrags:
cfrag = pre.reencrypt(kfrag=kfrag, capsule=capsule)
cfrags.append(cfrag) # Bob collects a cfrag
for cfrag in cfrags:
capsule.attach_cfrag(cfrag)
bob_cleartext = pre.decrypt(ciphertext=ciphertext,
capsule=capsule,
decrypting_key=bobs_private_key)
return bob_cleartext
@app.route('/demo2', methods=['POST'])
def demo2():
print("demo2!")
#################
# Generate Umbral keys for Alice.
################# []
# alices_private_key = keys.UmbralPrivateKey.gen_key()
alice_dirty_private_key = '0x16387146e0be3b7da18fc40e3c231c4d320d0e087c310ac49de1c178dde54398'
alices_private_key_hex = alice_dirty_private_key[-64:]
alices_private_key = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(alices_private_key_hex))
# bobs_private_key = keys.UmbralPrivateKey.gen_key()
bobs_dirty_private_key = "0xb6bb217955486f4cd9b2662fa909b0d4a207c8dae6f148be5e7ca94ed3fbdf0c"
bobs_private_key_hex = bobs_dirty_private_key[-64:]
bobs_private_key = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(bobs_private_key_hex))
n = 3
m = 2
alices_public_key, alices_signing_key, alices_verifying_key, bobs_public_key, kfrags = setup(alices_private_key, bobs_private_key, n, m)
################# ENCRYPT
# Encrypt data with Alice's public key.
################# []
plaintext = b'Proxy Re-Encryption is cool!'
ciphertext, capsule = en(alices_public_key, plaintext)
# Decrypt data with Alice's private key.
cleartext = pre.decrypt(ciphertext=ciphertext,
capsule=capsule,
decrypting_key=alices_private_key)
try:
bob_cleartext = de(capsule, alices_public_key, alices_verifying_key, ciphertext, bobs_private_key, kfrags[:m])
except:
return "can't decrypt", 400
assert bob_cleartext == plaintext
return 'demo2', 200
@app.route('/demo', methods=['POST'])
def demo():
print("demo!")
#################
# Generate Umbral keys for Alice.
################# []
alices_private_key = keys.UmbralPrivateKey.gen_key()
alices_public_key = alices_private_key.get_pubkey()
print("alices_private_key", alices_private_key)
alices_signing_key = keys.UmbralPrivateKey.gen_key()
alices_verifying_key = alices_signing_key.get_pubkey()
alices_signer = signing.Signer(private_key=alices_signing_key)
# Generate Umbral keys for Bob.
################# []
bobs_private_key = keys.UmbralPrivateKey.gen_key()
bobs_public_key = bobs_private_key.get_pubkey()
print("bobs_public_key", bobs_public_key)
################# [][]
n = 3
m = 2
# Alice generates "M of N" re-encryption key fragments (or "KFrags") for Bob.
# In this example, 10 out of 20.
kfrags = pre.generate_kfrags(delegating_privkey=alices_private_key,
signer=alices_signer,
receiving_pubkey=bobs_public_key,
threshold=m,
N=n)
################# ENCRYPT
# Encrypt data with Alice's public key.
################# []
plaintext = b'Proxy Re-Encryption is cool!'
ciphertext, capsule = pre.encrypt(alices_public_key, plaintext)
# Decrypt data with Alice's private key.
cleartext = pre.decrypt(ciphertext=ciphertext,
capsule=capsule,
decrypting_key=alices_private_key)
# Several Ursulas perform re-encryption, and Bob collects the resulting `cfrags`.
# He must gather at least `threshold` `cfrags` in order to activate the capsule.
capsule.set_correctness_keys(delegating=alices_public_key,
receiving=bobs_public_key,
verifying=alices_verifying_key)
cfrags = list() # Bob's cfrag collection
for kfrag in kfrags[:2]:
cfrag = pre.reencrypt(kfrag=kfrag, capsule=capsule)
cfrags.append(cfrag) # Bob collects a cfrag
# Bob activates and opens the capsule
for cfrag in cfrags:
capsule.attach_cfrag(cfrag)
bob_cleartext = pre.decrypt(ciphertext=ciphertext,
capsule=capsule,
decrypting_key=bobs_private_key)
assert bob_cleartext == plaintext
return 'demo', 200
@app.route('/encrypt', methods=['POST'])
def encrypt():
if not request.json :
abort(400)
bob_public_key = request.json['recipient']
pop_correct_public_key_hex = "03" + bob_public_key[-64:]
print(pop_correct_public_key_hex)
signedtext = request.json['data']
alices_signing_key = keys.UmbralPrivateKey.gen_key()
alices_verifying_key = alices_signing_key.get_pubkey()
alices_signer = signing.Signer(private_key=alices_signing_key)
#signedText
bob = keys.UmbralPublicKey.from_bytes(bytes.fromhex(pop_correct_public_key_hex))
ciphertext, capsule = pre.encrypt(bob, str.encode(signedtext))
#grants access to Bob
# kfrags = pre.generate_kfrags(delegating_privkey=alices_signing_key,
# signer=alices_signer,
# receiving_pubkey=bob,
# threshold=10,
# N=20)
#, "capsule":capsule
return jsonify(ciphertext = ciphertext.hex(), capsule = capsule.to_bytes().hex()), 200
@app.route('/decrypt', methods=['POST'])
def decrypt():
if not request.json :
abort(400)
bob_private_key = request.json['recipient']
pop_correct_private_key_hex = bob_private_key[-64:]
ciphertext = request.json['data']
capsule = request.json['capsule']
bobs = keys.UmbralPrivateKey.from_bytes(bytes.fromhex(pop_correct_private_key_hex))
cleartext = pre.decrypt(ciphertext=bytes.fromhex(ciphertext),
capsule=bytes.fromhex(capsule),
decrypting_key=bobs)
return jsonify(cleartext), 200
if __name__ == '__main__':
app.run(debug=True, port=5000)