def verify(sig, msg): global secret, E, l, P, sP # a = e_l(P, secret * Q) = e_l(P, Q) ^ secret a = symmetric_tate_pairing(E, P, sig, l) # Q = MapToPoint(m') # b = e_l(secret * P, Q) = e_l(P, Q) ^ secret h = int(hashlib.sha512(msg).hexdigest(), 16) b = symmetric_tate_pairing(E, sP, MapToPoint(E, E.field(h)), l) # a = b? return a == b
def encrypt(E, P, sP, pubkey, m, l): assert isinstance(m, (int, long)) # r = rand() r = random.randint(2**30, 2**31) # r*P, m xor e_l(secret * P, Q)^r = e_l(P, Q) ^ (secret * r) return (r * P, m ^ H(E.field(symmetric_tate_pairing(E, sP, pubkey, l) ** r)))
def check_time_key(self, Q, HT): """ 時刻サーバが配信するsH(T)が正しいものか確認する Args: Q: sH(T) HT: HT """ # left = e(Q,P) = e(sH(T), P) # print("tt=", T) left = self.H( self.E.field(symmetric_tate_pairing(self.E, Q, self.P, self.l))) # right = e(H(T),sP) right = self.H( self.E.field(symmetric_tate_pairing(self.E, HT, self.sP, self.l))) if left == right: print("Time key is correct") else: print("[NOTE] Time key is not correct !")
def decrypt(sHT0, C): """ 時刻サーバが配信してきたsHT0を用いて 暗号文Cを復号化 Args: sHT0:時刻鍵 (整数) C:(Enc(m), rP) """ sT0 = H(E.field(symmetric_tate_pairing(E, sHT0, C[1], l))) return C[0] ^ sT0
def decrypt_2(self, sHT0, Enc, rP): """ 時刻サーバが配信してきたsHT0を用いて 暗号文Cを復号化 Args: Enc: 暗号文 string rP : E上の点 string """ # rP = rP.decode("base64").decode("zlib") # rP = dill.loads(rP) #e(sH(T0), rP) sT0 = self.H( self.E.field(symmetric_tate_pairing(self.E, sHT0, rP, self.l))) return Enc ^ sT0
def decrypt(self, sHT0, C): """ 時刻サーバが配信してきたsHT0を用いて 暗号文Cを復号化 Args: sHT0:時刻鍵 (整数) C:(Enc(m), rP) """ #e(sH(T0), rP) try: sT0 = self.H( self.E.field(symmetric_tate_pairing(self.E, sHT0, C[1], self.l))) except: print("Error!") return C[0] ^ sT0
def encrypt(self, m, T0): """ 1.時刻を整数に変換 2.その整数を楕円曲線上の点に変換 3.その点を用いて暗号化鍵sT0をペアリング計算 4.sT0で暗号化 Args: T0:復号時刻 string m:平文 string """ # r = 2 r = random.randint(2**30, 2**31) # T0 = int(hashlib.sha512(T0).hexdigest().encode("hex"), 16) # 時刻を整数に T0 = (hashlib.sha512(T0.encode()).hexdigest()) T0 = binascii.hexlify(T0.encode()) T0 = int(T0, 16) T0 = self.time_to_ecc(T0) # 時刻からEに変換 sT0 = self.H( self.E.field( symmetric_tate_pairing(self.E, T0, r * self.sP, self.l))) # ペアリング計算 return (sT0 ^ m, r * self.P) # (整数, 楕円曲線上の点(射影座標表示))
def encrypt(m, T0): """ 1.時刻を整数に変換 2.その整数を楕円曲線上の点に変換 3.その点を用いて暗号化鍵sT0をペアリング計算 4.sT0で暗号化 Args: T0:復号時刻 string m:平文 string """ r = 2 # r = random.randint(2**30, 2**31) # 時刻を整数に T0 = (hashlib.sha512(T0.encode()).hexdigest()) T0 = binascii.hexlify(T0.encode()) T0 = int(T0, 16) # 時刻からEに変換 T0 = time_to_ecc(T0) # print("type T0", type(T0)) print("---------------------------\n", E, "\n", T0, "\n", r * secret * P, "\n", l, "\n") print("l =", l) # ペアリング計算 sT0 = H(E.field(symmetric_tate_pairing(E, T0, r * secret * P, l))) return (sT0 ^ m, r * P) # (整数, 楕円曲線上の点(射影座標表示))
def decrypt(E, K, c, l): # c1, c2 = r*P, m xor e_l(secret * P, Q) ^ r = e_l(P, Q) ^ (secret * r) # a = e_l(c1, K) = e_l(r*P, secret * Q) = e_l(P, Q) ^ (secret * r) print symmetric_tate_pairing(E, c[0], K, l) return c[1] ^ H(E.field(symmetric_tate_pairing(E, c[0], K, l)))
def encrypt(E, P, sP, pubkey, m, l): assert isinstance(m, (int, long)) # r = rand() r = random.randint(2**30, 2**31) # r*P, m xor e_l(secret * P, Q)^r = e_l(P, Q) ^ (secret * r) return (r * P, m ^ H(E.field(symmetric_tate_pairing(E, sP, pubkey, l)**r)))
def decrypt(E, K, c, l): # c1, c2 = r*P, m xor e_l(secret * P, Q) ^ r = e_l(P, Q) ^ (secret * r) # a = e_l(c1, K) = e_l(r*P, secret * Q) = e_l(P, Q) ^ (secret * r) return c[1] ^ H(E.field(symmetric_tate_pairing(E, c[0], K, l)))