-
Notifications
You must be signed in to change notification settings - Fork 0
/
dAES-tests.py
executable file
·150 lines (130 loc) · 6.32 KB
/
dAES-tests.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
147
148
149
150
#!/usr/bin/env python
from __future__ import division
import dAES
import uuid, random, copy, timeit, sys
class colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def mean(stuff):
return sum(stuff)/len(stuff)
def doubler(start, num):
listy = []
listy.append(start)
for i in range(num-1):
listy.append(listy[i]*2)
return listy
def sboxDiff(SboxIn, SboxOut):
diffBytes = 0
for b, byte in enumerate(SboxIn):
if not SboxOut[b] == byte:
diffBytes += 1
return diffBytes/len(SboxIn)
def bitFlip(key):
flippedKeys = []
for i in range(0, len(key), 2):
bits = bin(int(key[i:i+2], base=16))
for j, b in enumerate(bits[2:len(bits)]):
flip = list(copy.deepcopy(bits[2:len(bits)]))
flip[j] = (1)^int(b)
bitStr = ""
for f in flip:
bitStr += str(f)
flippedKey = copy.deepcopy(key)
if not flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4] in flippedKeys and not flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4]+flippedKey[i:len(flippedKey)] in flippedKeys:
if len(flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4]) == len(key):
flippedKeys.append(flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4])#+flippedKey[i+2:len(flippedKey)])
elif len(flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4]+flippedKey[i:len(flippedKey)]) == len(key) :
flippedKeys.append(flippedKey[0:i-2]+hex(int(bitStr, base=2))[2:4]+flippedKey[i:len(flippedKey)])
return flippedKeys
# Basic encryption tests
def basic_tests():
print(colors.HEADER+"[Basic encryption tests]"+colors.ENDC)
test_key = uuid.uuid4().hex+uuid.uuid4().hex # 256-bit (64 character) hex key
print(colors.OKBLUE+"Generated 256-bit AES key: "+colors.WARNING+test_key+colors.ENDC)
print(colors.OKBLUE+"Checking that two S-Boxes generated from the same key are equal..."+colors.ENDC)
sbox_one = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(test_key))
sbox_two = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(test_key))
if not sbox_one==sbox_two:
print(colors.OKBLUE+"\tS-Boxes aren't the same! "+colors.FAIL+"[FAILED]"+colors.ENDC)
exit(1)
print(colors.OKBLUE+"\tS-Boxes are the same "+colors.OKGREEN+"[PASSED]"+colors.ENDC)
int_test_key = dAES.hexToKey(test_key)
plain = uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex[0:random.randrange(0,32)]
plaintwo = uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex+uuid.uuid4().hex[0:random.randrange(0,32)]
print(colors.OKBLUE+"Generating "+str(len(plain))+" bytes of plaintext..."+colors.ENDC)
print(colors.OKBLUE+"Encrypting plaintext..."+colors.ENDC)
hard = dAES.encrypt(plain, int_test_key)
hardtwo = dAES.encrypt(plain, int_test_key)
if plain!=hard:
print(colors.OKBLUE+"\tEncrypted ciphertext isn't the same as plaintext! "+colors.OKGREEN+"[PASSED]"+colors.ENDC)
else:
print(colors.OKBLUE+"\tEncrypted ciphertext is the same as plaintext! "+colors.FAIL+"[FAILED]"+colors.ENDC)
exit(1)
print(colors.OKBLUE+"Decrypting ciphertext..."+colors.ENDC)
decd = dAES.decrypt(hard, int_test_key)
if plain==decd:
print(colors.OKBLUE+"\tDecrypted plaintext is the same as original plaintext! "+colors.OKGREEN+"[PASSED]"+colors.ENDC)
else:
print(colors.OKBLUE+"\tDecrypted plaintext isn't the same as original plaintext! "+colors.FAIL+"[FAILED]"+colors.ENDC)
exit(1)
print(colors.HEADER+"## All basic tests passed ##"+colors.ENDC)
# testing speed of enc and dec functions
def speed_tests(size):
eSpeeds = []
dSpeeds = []
test_key = uuid.uuid4().hex+uuid.uuid4().hex # 256-bit (64 character) hex key
plain = uuid.uuid4().hex*(size//32)
e = timeit.Timer("dAES.encrypt('"+plain+"', dAES.hexToKey(\""+test_key+"\"))", "import dAES")
eSpeeds = e.repeat(100, 1)
if hasattr(sys, "pypy_translation_info"):
print(colors.OKBLUE+"\tMean "+colors.OKGREEN+"enc"+colors.OKBLUE+" speed for "+str(len(plain))+" character string: "+colors.HEADER+str(round(mean(eSpeeds), 6))+"s"+colors.ENDC)
else:
print(colors.OKBLUE+"\tMean "+colors.OKGREEN+"enc"+colors.OKBLUE+" speed for "+str(sys.getsizeof(plain))+" byte string: "+colors.HEADER+str(round(mean(eSpeeds), 6))+"s"+colors.ENDC)
d = timeit.Timer("dAES.decrypt(\""+plain+"\", dAES.hexToKey(\""+test_key+"\"))", "import dAES")
dSpeeds = e.repeat(100, 1)
if hasattr(sys, "pypy_translation_info"):
print(colors.OKBLUE+"\tMean "+colors.FAIL+"dec"+colors.OKBLUE+" speed for "+str(len(plain))+" character string: "+colors.HEADER+str(round(mean(dSpeeds), 6))+"s\n"+colors.ENDC)
else:
print(colors.OKBLUE+"\tMean "+colors.FAIL+"dec"+colors.OKBLUE+" speed for "+str(sys.getsizeof(plain))+" byte string: "+colors.HEADER+str(round(mean(dSpeeds), 6))+"s\n"+colors.ENDC)
return [eSpeeds, dSpeeds]
# testing how different s-boxes actually are...
def sbox_tests(iters):
# sbox tests
print(colors.HEADER+"\n[Dynamic S-Box tests]"+colors.ENDC)
test_key = uuid.uuid4().hex+uuid.uuid4().hex
sbox_sim = []
for i in range(iters):
sbox_key = uuid.uuid4().hex+uuid.uuid4().hex
sbox_keys = bitFlip(sbox_key)
sbox_clean = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(sbox_key))
while len(sbox_keys) > 0:
k = random.choice(sbox_keys)
sbox_test = dAES.generateDynamicSbox(dAES.sboxOrig, dAES.hexToKey(k))
diff = sboxDiff(sbox_clean, sbox_test)
sbox_sim.append(diff)
sbox_keys.pop(sbox_keys.index(k))
print(colors.OKBLUE+"Tested "+str(len(sbox_sim))+" S-Boxes"+colors.ENDC)
print(colors.OKBLUE+"\tMean difference: "+colors.OKGREEN+str(round(mean(sbox_sim)*100, 2))+"%"+colors.ENDC)
print(colors.OKBLUE+"\tPercentage with no difference: "+colors.OKGREEN+str(round((sbox_sim.count(0)/len(sbox_sim))*100, 2))+"%"+colors.ENDC)
return sbox_sim
if hasattr(sys, "pypy_translation_info"):
print(colors.HEADER+"\t\t\t[pypy tests]"+colors.ENDC)
else:
print(colors.HEADER+"\t\t\t[python tests]"+colors.ENDC)
basic_tests()
sbox_tests(100)
print(colors.HEADER+"\n[Speed tests]"+colors.ENDC)
if hasattr(sys, "pypy_translation_info"):
doubles = doubler(32768, 5)
print(colors.OKBLUE+"Testing encryption and decryption speeds for strings of length ["+colors.OKGREEN+", ".join(map(str, doubles))+colors.OKBLUE+"]"+colors.ENDC)
for i in doubles:
speed_tests(i)
else:
doubles = doubler(32768, 2)
print(colors.OKBLUE+"Testing encryption and decryption speeds for strings of length ["+colors.OKGREEN+", ".join(map(str, doubles))+colors.OKBLUE+"]"+colors.ENDC)
for i in doubles:
speed_tests(i)