-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_ECC.py
146 lines (111 loc) · 5.76 KB
/
run_ECC.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#
# Author: Alexander Craig
# Project: An Analysis of the Security of RSA & Elliptic Curve Cryptography
# Supervisor: Maximilien Gadouleau
# Version: 2.0
# Date: 06/02/19
#
# Functionality: utilises other programs to generate and subsequently break ECC
# keys using a variety of algorithms, while collecting diagnostics
# to compare and check the results of each of these algorithms
#
# Instructions: used to run all other files to be run from the command line:
#
# CLI: python3 run_ECC.py -h (to see possible flags)
#
############ IMPORTS #########
# needed for pydocs to correctly find everything
import sys
sys.path.append('Programming/')
import argparse
from ECC import *
############ FUNCTIONS #########
def runSolver(keys, solver, name, verbose):
""" runs a check on the solver, given the correct keys """
if verbose:
print("="*10, name, "="*10)
solver.solve() # Elliptic Curve discrete log problem
if verbose:
if solver.k == keys.k: # check for correctness
print("Success!")
else:
print("Fail!")
return {"res": (solver.k == keys.k), # return result as dict
"time": solver.time,
"count": solver.count}
############ MASTER PROGRAM #########
def run(k = 10, brute = True, babyStep = True, rho = True,
lamb = True, poHel = True, movAttack = True, verbose = True):
""" creates a k-bit ECC key, cracks it with several algorithms, and generates
statistics to compare their performance """
############ KEY GENERATION #########
if verbose:
print("\n" + "="*10, "GENERATING", "="*10)
keys = generate_ECC.KeyGen(k, verbose) # create new instance
keys.generateCurve() # find a good curve
sanity = keys.generateKeys() # get key and primes
if not sanity:
if verbose:
print("Please fix input and try again")
return False
############ BRUTE FORCE ATTACK #########
bf_res = {}
if brute:
bf = brute_force.BFSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
bf_res = runSolver(keys, bf, "BRUTE FORCE", verbose) # check solver
############ BABYSTEP-GIANTSTEP ATTACK #########
bsgs_res = {}
if babyStep:
bg = baby_step.BGSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
bsgs_res = runSolver(keys, bg, "BABYSTEP_GIANTSTEP", verbose) # check solver
############ POLLARD'S RHO ATTACK #########
rho_res = {}
if rho:
rhoS = pollard_rho.PRSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
rho_res = runSolver(keys, rhoS, "POLLARD'S RHO", verbose) # check solver
############ POLLARD'S RHO ATTACK #########
lambda_res = {}
if lamb:
lambSol = pollard_lambda.PLSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
lambda_res = runSolver(keys, lambSol, "POLLARD'S LAMBDA", verbose) # check solver
############ POHLIG HELLMAN ATTACK #########
poh_res = {}
if poHel:
pohSol = pohlig_hellman.PHSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
poh_res = runSolver(keys, pohSol, "POHLIG HELLMAN", verbose) # check solver
############ MOV ATTACK #########
mov_res = {}
if movAttack:
movSol = mov_attack.MOVSolver(keys.curve, keys.Q, keys.G, verbose) # create new instance with public key info
mov_res = runSolver(keys, movSol, "MOV ATTACK", verbose) # check solver
return bf_res, bsgs_res, rho_res, lambda_res, poh_res, mov_res
def test(k = 10):
""" tries to find failure point """
res = {}
res['res'] = False
# loop till success
while not res['res']:
res = run(k, False, False, False, False, False, True, verbose = True)[-1]
############ COMMAND LINE INTERFACE #########
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="turns output off", action="store_true")
parser.add_argument("-k", "--bitsize", help="bitlength of public key", action="store", type=int, default=10)
parser.add_argument("-bf", "--bruteforce", help="turns bruteforce decryption on", action="store_true")
parser.add_argument("-bs", "--baby_step", help="turns baby_step-giant_step decryption on", action="store_true")
parser.add_argument("-pr", "--pollard_rho", help="turns pollard_rho decryption on", action="store_true")
parser.add_argument("-pl", "--pollard_lambda", help="turns pollard_lambda decryption on", action="store_true")
parser.add_argument("-ph", "--pohlig_hellman", help="turns pohlig_hellman decryption on", action="store_true")
parser.add_argument("-ma", "--mov_attack", help="turns mov_attack decryption on", action="store_true")
parser.add_argument("-a", "--all", help="turns all on", action="store_true")
parser.add_argument("-t", "--test", help="runs failure test", action="store_true")
args = parser.parse_args()
if args.test:
test(args.bitsize)
elif len(sys.argv) == 1:
# default run
run()
elif args.all:
run(args.bitsize, True, True, True, True, True, True, not args.verbose)
else:
run(args.bitsize, args.bruteforce, args.baby_step, args.pollard_rho, args.pollard_lambda, args.pohlig_hellman, args.mov_attack, not args.verbose)