forked from jiangtaoluo/IB-PRE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ible.py
96 lines (70 loc) · 2.25 KB
/
ible.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
#!/usr/bin/python3
# Procedures:
# (ID, sk_ID) = GenKey
# C = AES(m, k)
# ck = IBE(k, ID)
# ......
# k = IBD(ck, sk_ID)
# m = AES(C, k)
import time
from charm.toolbox.pairinggroup import PairingGroup, GT,ZR,G1,G2
from charm.toolbox.ecgroup import ECGroup
from charm.toolbox.eccurve import prime192v2, prime192v1
from charm.schemes.pkenc.pkenc_cs98 import CS98
# For AES
from charm.toolbox.pairinggroup import extract_key
from charm.toolbox.symcrypto import SymmetricCryptoAbstraction
from charm.core.math.pairing import hashPair as sha1
from charm.core.math.pairing import pair
# mg07a
from pre_mg07a_jet import PreGA
# IBPRE function
# pre: IB-PRE cipher generated outside
# msg: the original message in bytes, e.g. b"Hello, world"
# ID: Receiver's ID
# k: symmetric key for message
def ible(pre, ID, msg, k, master_key, params, log_file):
if type(msg) != bytes: raise "Message type error: msg should be bytes."
# switch for display
#debug = True
debug = False
if debug:
print("\nThe original message is: \n", msg)
# Generate the Sym Cipher initilized by k
symCipher = SymmetricCryptoAbstraction(k)
# Symmetric Encrypt: AES(CTR) default
t1 = time.time()
ct = symCipher.encrypt(msg)
t2 = time.time()
t_aes = (t2-t1)*1000
log = '{0:.4f}'.format(t_aes)+"\t"
# Generate private keys for ID
sk1 = pre.keyGen(master_key, ID)
# Encrypt 'Sym k' using ID
t1 = time.time()
ck = pre.encrypt_jet(params, ID, k)
t2 = time.time()
t_ibe = (t2-t1)*1000
log += '{0:.4f}'.format(t_ibe) + "\t"
# decryption using sk1
t1 = time.time()
dk =pre.decrypt_jet(params, sk1, ck)
t2 = time.time()
t_ibd = (t2-t1)*1000
log += '{0:.4f}'.format(t_ibd) + "\t"
if debug:
print("\nThe decrypted key is:\n", dk)
# Transmit dk and ct to the receipt
# ........
# Sym decrypt using the IBD output
decipher = SymmetricCryptoAbstraction(dk)
t1 = time.time()
dmsg = decipher.decrypt(ct)
t2 = time.time()
t_aes2 = (t2-t1)*1000
log += '{0:.4f}'.format(t_aes2)+"\n"
if debug:
print("\nThe decrypted message is:\n", dmsg)
assert dmsg == msg , 'o: =>%s\nm: =>%s' % (msg, dmsg)
# output log
log_file.write(log)