Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ashutosh1206
GitHub Repository: ashutosh1206/crypton
Path: blob/master/Identification/Ephemeral-Key-Auth/Challenges/EC-Auth/ecsession.py
1403 views
1
#!/usr/bin/env python2.7
2
3
from Crypto.Util.number import *
4
from os import urandom
5
import sys
6
import ecauth
7
from secret import flag, n
8
9
class Unbuffered(object):
10
def __init__(self, stream):
11
self.stream = stream
12
def write(self, data):
13
self.stream.write(data)
14
self.stream.flush()
15
def writelines(self, datas):
16
self.stream.writelines(datas)
17
self.stream.flush()
18
def __getattr__(self, attr):
19
return getattr(self.stream, attr)
20
21
sys.stdout = Unbuffered(sys.stdout)
22
23
class colors:
24
reset='\033[0m'
25
red='\033[31m'
26
green='\033[32m'
27
orange='\033[33m'
28
blue='\033[34m'
29
30
31
p = 89953523493328636138979614835438769105803101293517644103178299545319142490503
32
a = p-3
33
b = 28285296545714903834902884467158189217354728250629470479032309603102942404639
34
ec = ecauth.CurveFp(p, a, b)
35
36
# Point P on the curve
37
_Px = 0x337ef2115b4595fbd60e2ffb5ee6409463609e0e5a6611b105443e02cb82edd8L
38
_Py = 0x1879b8d7a68a550f58166f0d6b4e86a0873d7b709e28ee318ddadd4ccf505e1aL
39
40
# n is the order of the subgroup generated by (_Px, _Py)
41
assert ec.contains_point(_Px, _Py)
42
43
# x is the session-secret key
44
x = getPrime(254)
45
46
assert x < n
47
48
point_P = ecauth.Point(ec, _Px, _Py, n)
49
assert point_P * n == ecauth.INFINITY
50
51
publickey = ecauth.Public_key(point_P)
52
privatekey = ecauth.Private_key(publickey, x)
53
54
"""
55
You can use the _sign method in ecauth to sign for authentication process.
56
But remember,
57
You must have the same x as generated in this script to successfully authenticate!
58
"""
59
60
print colors.orange + "-"*26 + "Welcome to EC-Auth mechanism" + "-"*26 + colors.reset
61
point_Q = x * point_P
62
print "\nHere is my point Q = x * P: ", point_Q
63
64
try:
65
_Rx = raw_input("\nGive me x-coordinate of point R = r * P (in hex without 0x): ")
66
_Ry = raw_input("Give me y-coordinate of point R = r * P (in hex without 0x): ")
67
s = raw_input("Give me s = r + x (in hex without 0x): ")
68
_Rx = int(_Rx, 16)
69
_Ry = int(_Ry, 16)
70
s = int(s, 16)
71
except:
72
print colors.red + "\nEnter proper hex values!" + colors.reset
73
sys.exit()
74
75
if not ec.contains_point(_Rx, _Ry):
76
print colors.red + "\nPoint R does not lie on the curve!" + colors.reset
77
sys.exit()
78
79
point_R = ecauth.Point(ec, _Rx, _Ry)
80
assert n * point_R == ecauth.INFINITY
81
82
obj1 = ecauth.Handshake(point_Q, point_R, s)
83
if publickey._verify(obj1):
84
print colors.green + "\nHere, take your flag: " + flag + colors.reset
85
else:
86
print colors.red + "\nI knew you would fail" + colors.reset
87
sys.exit()
88
89