forked from stefan-contiu/trusted-sharing
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bdcst_enc.py
58 lines (45 loc) · 1.61 KB
/
bdcst_enc.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
from ctypes import cdll
from ctypes import c_int, c_char_p, byref, create_string_buffer, POINTER
from crypto import PKI
from wrap_openssl import OpenSSLWrapper
lib = cdll.LoadLibrary('./pbc_bce-0.0.1/bdcst.so')
class BroadcastEncryption(object):
def __init__(self):
self.openssl = OpenSSLWrapper()
def setup(self):
lib.one_time_init()
def encrypt(self, users):
u = (c_int * len(users))()
for i in range(len(users)):
u[i] = PKI.get_index(users[i])
k = create_string_buffer(512)
c = create_string_buffer(512)
k_len = (c_int * 1)()
c_len = (c_int * 1)()
lib.broadcast_encrypt_group(u, len(users), k, k_len, c, c_len)
kb = bytes(k)[:k_len[0]]
cb = bytes(c)[:c_len[0]]
return (self.openssl.sha256(kb), cb)
def decrypt(self, users, current_user, c):
u = (c_int * len(users))()
for i in range(len(users)):
u[i] = PKI.get_index(users[i])
pri_key_file_name = 'keys_32/user_' + str(PKI.get_index(current_user)) + ".key"
p = create_string_buffer(bytes(pri_key_file_name, 'utf-8'))
k = create_string_buffer(512)
k_len = (c_int * 1)()
lib.broadcast_decrypt_group(u, len(users), p,
c, len(c), k, k_len)
kb = bytes(k)[:k_len[0]]
return self.openssl.sha256(kb)
def main():
b = BroadcastEncryption()
#b.setup()
# alice, bob, eve, steve
u = ["alice", "steve"]
(k, c) = b.encrypt(u)
kd = b.decrypt(u, "alice", c)
assert k == kd
print("Broadcast encryption basic test works!")
if __name__ == "__main__":
main()