-
Notifications
You must be signed in to change notification settings - Fork 0
/
YAHK14.py
103 lines (90 loc) · 5.17 KB
/
YAHK14.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
# Required imports from the library:
from charm.toolbox.ABEnc import ABEnc
from charm.schemes.abenc.abenc_unmcpabe_yahk14 import CPABE_YAHK14
from charm.toolbox.symcrypto import AuthenticatedCryptoAbstraction
from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
from charm.core.math.pairing import hashPair as sha2
import timeit
def main():
# instantiate a bilinear pairing map
groupObj = PairingGroup('SS512')
cpabe = CPABE_YAHK14(groupObj)
# run the set up
def my_setup ():
(pk, msk) = cpabe.setup()
# Measure the time taken to run the setup. We run the function 500 times and get the total time required.
# It is then divided by 500 again to get the mean time for each execution
time_setup= (timeit.timeit(setup = "gc.enable()",
stmt = my_setup,
number = 500))/500
print ("Mean time per iteration for setup [s] ->", time_setup)
# run the key generation. attr_list can contain as many attributes as desired. However, more attributes require more time.
def my_keygen ():
(pk, msk) = cpabe.setup()
attr_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
key_abe = cpabe.keygen(pk, msk, attr_list)
# Measure the time taken to run keygen
time_setup_keygen = (timeit.timeit(setup = "gc.enable()",
stmt = my_keygen,
number = 500))/500
time_keygen = time_setup_keygen - time_setup
print ("Mean time per iteration for keygen [s] ->", time_keygen)
# run the ABE encryption. policy_str can contain as many attributes as desired.
# AND policies are always more time-consuming than OR policies. More attributes also mean more time.
# In this scheme, ABE is used to encrypt a the symmetric key that will later be used to encrypt the actual message.
def my_ABE_Enc ():
(pk, msk) = cpabe.setup()
key_sym_seed = groupObj.random(GT)
policy_str = '(1 and 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9 and 10 and 11 and 12 and 13 and 14 and 15)' # policy for ABE
CTabe = cpabe.encrypt(pk, key_sym_seed, policy_str) #encryption
time_setup_ABEnc = (timeit.timeit(setup = "gc.enable()",
stmt = my_ABE_Enc,
number = 500))/500
time_ABEnc = time_setup_ABEnc - time_setup
print ("Mean time per iteration for ABE encryption [s] ->", time_ABEnc)
# run the ABE encryption. policy_str can contain as many attributes as desired.
# AND policies are always more time-consuming than OR policies. More attributes also mean more time.
def my_ABE_Dec ():
(pk, msk) = cpabe.setup()
attr_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']
key_abe = cpabe.keygen(pk, msk, attr_list)
key_sym_seed = groupObj.random(GT)
policy_str = '(1 and 2 and 3 and 4 and 5 and 6 and 7 and 8 and 9 and 10 and 11 and 12 and 13 and 14 and 15)' # policy for ABE
CTabe = cpabe.encrypt(pk, key_sym_seed, policy_str) #encryption
key_sym_seed_dec = cpabe.decrypt(pk, key_abe, CTabe)
time_setup_keygen_ABEnc_ABEdec = (timeit.timeit(setup = "gc.enable()",
stmt = my_ABE_Dec,
number = 500))/500
time_ABEdec = time_setup_keygen_ABEnc_ABEdec - time_setup_ABEnc - time_keygen
print ("Mean time per iteration for ABE Decryption [s] ->", time_ABEdec)
# run the symmetric encryption.
# Time required to encrypt the message will depend on the size of said message
def my_SymEnc ():
key_sym_seed = groupObj.random(GT)
msg = b"hello world this is an important message."
cipher_enc = AuthenticatedCryptoAbstraction(sha2(key_sym_seed)) #AES instantiation with key_sym_seed
CTaes = cipher_enc.encrypt(msg) # AES message encryption
time_symenc = (timeit.timeit(setup = "gc.enable()",
stmt = my_SymEnc,
number = 500))/500
print ("Mean time per iteration for symmetric encryption [s] ->", time_symenc)
# run the symmetric decryption.
# Time required to decrypt the message will depend:
# - on the size of said message
# - on the complexity of the ABE policy used to encrypt the symmetric key (i.e., the amoun of attributes in the policy)
def my_SymDec ():
key_sym_seed = groupObj.random(GT)
msg = b"hello world this is an important message."
cipher_enc = AuthenticatedCryptoAbstraction(sha2(key_sym_seed)) #AES instantiation with key_sym_seed
CTaes = cipher_enc.encrypt(msg) # AES message encryption
cipher_dec = AuthenticatedCryptoAbstraction(sha2(key_sym_seed))
msg_dec = cipher_dec.decrypt(CTaes)
assert msg_dec == msg, "Failed AES Decryption!"
timecomplete = (timeit.timeit(setup = "gc.enable()",
stmt = my_SymDec,
number = 500))/500
time_symdec = timecomplete - time_symenc
print ("Mean time per iteration for symmetric decryption [s] ->", time_symdec)
if __name__ == "__main__":
debug = True
main()