Path: blob/master/Identification/Ephemeral-Key-Auth/Challenges/EC-Auth/ecsession.py
1403 views
#!/usr/bin/env python2.712from Crypto.Util.number import *3from os import urandom4import sys5import ecauth6from secret import flag, n78class Unbuffered(object):9def __init__(self, stream):10self.stream = stream11def write(self, data):12self.stream.write(data)13self.stream.flush()14def writelines(self, datas):15self.stream.writelines(datas)16self.stream.flush()17def __getattr__(self, attr):18return getattr(self.stream, attr)1920sys.stdout = Unbuffered(sys.stdout)2122class colors:23reset='\033[0m'24red='\033[31m'25green='\033[32m'26orange='\033[33m'27blue='\033[34m'282930p = 8995352349332863613897961483543876910580310129351764410317829954531914249050331a = p-332b = 2828529654571490383490288446715818921735472825062947047903230960310294240463933ec = ecauth.CurveFp(p, a, b)3435# Point P on the curve36_Px = 0x337ef2115b4595fbd60e2ffb5ee6409463609e0e5a6611b105443e02cb82edd8L37_Py = 0x1879b8d7a68a550f58166f0d6b4e86a0873d7b709e28ee318ddadd4ccf505e1aL3839# n is the order of the subgroup generated by (_Px, _Py)40assert ec.contains_point(_Px, _Py)4142# x is the session-secret key43x = getPrime(254)4445assert x < n4647point_P = ecauth.Point(ec, _Px, _Py, n)48assert point_P * n == ecauth.INFINITY4950publickey = ecauth.Public_key(point_P)51privatekey = ecauth.Private_key(publickey, x)5253"""54You can use the _sign method in ecauth to sign for authentication process.55But remember,56You must have the same x as generated in this script to successfully authenticate!57"""5859print colors.orange + "-"*26 + "Welcome to EC-Auth mechanism" + "-"*26 + colors.reset60point_Q = x * point_P61print "\nHere is my point Q = x * P: ", point_Q6263try:64_Rx = raw_input("\nGive me x-coordinate of point R = r * P (in hex without 0x): ")65_Ry = raw_input("Give me y-coordinate of point R = r * P (in hex without 0x): ")66s = raw_input("Give me s = r + x (in hex without 0x): ")67_Rx = int(_Rx, 16)68_Ry = int(_Ry, 16)69s = int(s, 16)70except:71print colors.red + "\nEnter proper hex values!" + colors.reset72sys.exit()7374if not ec.contains_point(_Rx, _Ry):75print colors.red + "\nPoint R does not lie on the curve!" + colors.reset76sys.exit()7778point_R = ecauth.Point(ec, _Rx, _Ry)79assert n * point_R == ecauth.INFINITY8081obj1 = ecauth.Handshake(point_Q, point_R, s)82if publickey._verify(obj1):83print colors.green + "\nHere, take your flag: " + flag + colors.reset84else:85print colors.red + "\nI knew you would fail" + colors.reset86sys.exit()878889