-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathaes.py
97 lines (72 loc) · 7.52 KB
/
aes.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
# At 0x439
CONST = [0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, ]
def fn4(round_no, state, key): # Self reversible
for i in range(16):
state[i] ^= key[i] ^ CONST[i + round_no * 16]
def fn3(state): # Self reversible
for i in range(0, 16, 4):
x0 = state[0 + i]
x1 = state[1 + i]
x2 = state[2 + i]
x3 = state[3 + i]
state[0 + i] = x1 ^ x2 ^ x3
state[1 + i] = x0 ^ x2 ^ x3
state[2 + i] = x0 ^ x1 ^ x3
state[3 + i] = x0 ^ x1 ^ x2
def fn2(state):
new = [0] * 16
for i, j in enumerate([0, 0xa, 5, 0xf, 0xe, 4, 0xb, 1, 9, 3, 0xc, 6, 7, 0xd, 2, 8]):
new[i] = state[j]
for i in range(16):
state[i] = new[i]
def un2(state):
new = [0] * 16
for j, i in enumerate([0, 0xa, 5, 0xf, 0xe, 4, 0xb, 1, 9, 3, 0xc, 6, 7, 0xd, 2, 8]):
new[i] = state[j]
for i in range(16):
state[i] = new[i]
import subprocess
def fun(round, v24):
p = subprocess.Popen(["simavr/simavr/run_avr",
"/home/adam/VirtualBoxVMs/SharedFolder/MatrixLED.ino.elf",
"-m", "atmega32u4"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write("%d %d\n" % (v24, round))
p.wait()
for line in p.stdout.readlines():
if line.startswith("Res:"):
return int(line.split()[1])
FX = [[17, 16, 81, 80, 180, 48, 244, 112, 89, 88, 25, 24, 252, 120, 188, 56, 1, 0, 19, 18, 164, 32, 182, 50, 11, 10, 27, 26, 174, 42, 190, 58, 21, 49, 85, 113, 181, 53, 245, 117, 93, 121, 29, 57, 253, 125, 189, 61, 5, 33, 23, 51, 165, 37, 183, 55, 15, 43, 31, 59, 175, 47, 191, 63, 75, 74, 91, 90, 238, 106, 254, 122, 73, 72, 65, 64, 236, 104, 228, 96, 3, 2, 83, 82, 166, 34, 246, 114, 9, 8, 67, 66, 172, 40, 230, 98, 79, 107, 95, 123, 239, 111, 255, 127, 77, 105, 69, 97, 237, 109, 229, 101, 7, 35, 87, 115, 167, 39, 247, 119, 13, 41, 71, 99, 173, 45, 231, 103, 149, 176, 213, 240, 148, 144, 212, 208, 221, 248, 157, 184, 220, 216, 156, 152, 133, 160, 151, 178, 132, 128, 150, 146, 143, 170, 159, 186, 142, 138, 158, 154, 145, 177, 209, 241, 20, 52, 84, 116, 217, 249, 153, 185, 92, 124, 28, 60, 129, 161, 147, 179, 4, 36, 22, 54, 139, 171, 155, 187, 14, 46, 30, 62, 207, 234, 223, 250, 206, 202, 222, 218, 205, 232, 197, 224, 204, 200, 196, 192, 135, 162, 215, 242, 134, 130, 214, 210, 141, 168, 199, 226, 140, 136, 198, 194, 203, 235, 219, 251, 78, 110, 94, 126, 201, 233, 193, 225, 76, 108, 68, 100, 131, 163, 211, 243, 6, 38, 86, 118, 137, 169, 195, 227, 12, 44, 70, 102], [136, 138, 75, 203, 172, 174, 111, 239, 128, 130, 67, 195, 148, 150, 87, 215, 168, 170, 107, 235, 140, 142, 79, 207, 152, 154, 91, 219, 156, 158, 95, 223, 180, 182, 119, 247, 164, 166, 103, 231, 144, 146, 83, 211, 132, 134, 71, 199, 188, 190, 127, 255, 160, 162, 99, 227, 184, 186, 123, 251, 176, 178, 115, 243, 202, 200, 74, 10, 238, 236, 110, 46, 194, 192, 66, 2, 214, 212, 86, 22, 234, 232, 106, 42, 206, 204, 78, 14, 218, 216, 90, 26, 222, 220, 94, 30, 246, 244, 118, 54, 230, 228, 102, 38, 210, 208, 82, 18, 198, 196, 70, 6, 254, 252, 126, 62, 226, 224, 98, 34, 250, 248, 122, 58, 242, 240, 114, 50, 8, 137, 9, 139, 44, 173, 45, 175, 0, 129, 1, 131, 20, 149, 21, 151, 40, 169, 41, 171, 12, 141, 13, 143, 24, 153, 25, 155, 28, 157, 29, 159, 52, 181, 53, 183, 36, 165, 37, 167, 16, 145, 17, 147, 4, 133, 5, 135, 60, 189, 61, 191, 32, 161, 33, 163, 56, 185, 57, 187, 48, 177, 49, 179, 73, 201, 72, 11, 109, 237, 108, 47, 65, 193, 64, 3, 85, 213, 84, 23, 105, 233, 104, 43, 77, 205, 76, 15, 89, 217, 88, 27, 93, 221, 92, 31, 117, 245, 116, 55, 101, 229, 100, 39, 81, 209, 80, 19, 69, 197, 68, 7, 125, 253, 124, 63, 97, 225, 96, 35, 121, 249, 120, 59, 113, 241, 112, 51], [68, 195, 71, 67, 64, 192, 194, 66, 84, 211, 87, 83, 80, 208, 210, 82, 60, 187, 63, 59, 56, 184, 186, 58, 124, 251, 127, 123, 120, 248, 250, 122, 116, 243, 119, 115, 112, 240, 242, 114, 100, 227, 103, 99, 96, 224, 226, 98, 52, 179, 55, 51, 48, 176, 178, 50, 20, 147, 23, 19, 16, 144, 146, 18, 4, 131, 7, 3, 0, 128, 130, 2, 76, 203, 79, 75, 72, 200, 202, 74, 12, 139, 15, 11, 8, 136, 138, 10, 92, 219, 95, 91, 88, 216, 218, 90, 44, 171, 47, 43, 40, 168, 170, 42, 108, 235, 111, 107, 104, 232, 234, 106, 36, 163, 39, 35, 32, 160, 162, 34, 28, 155, 31, 27, 24, 152, 154, 26, 69, 199, 70, 65, 196, 197, 198, 193, 85, 215, 86, 81, 212, 213, 214, 209, 61, 191, 62, 57, 188, 189, 190, 185, 125, 255, 126, 121, 252, 253, 254, 249, 117, 247, 118, 113, 244, 245, 246, 241, 101, 231, 102, 97, 228, 229, 230, 225, 53, 183, 54, 49, 180, 181, 182, 177, 21, 151, 22, 17, 148, 149, 150, 145, 5, 135, 6, 1, 132, 133, 134, 129, 77, 207, 78, 73, 204, 205, 206, 201, 13, 143, 14, 9, 140, 141, 142, 137, 93, 223, 94, 89, 220, 221, 222, 217, 45, 175, 46, 41, 172, 173, 174, 169, 109, 239, 110, 105, 236, 237, 238, 233, 37, 167, 38, 33, 164, 165, 166, 161, 29, 159, 30, 25, 156, 157, 158, 153], [34, 43, 32, 41, 162, 171, 38, 47, 75, 11, 73, 9, 203, 139, 79, 15, 178, 187, 52, 61, 50, 59, 54, 63, 219, 155, 93, 29, 91, 27, 95, 31, 2, 67, 0, 65, 130, 195, 6, 71, 66, 3, 64, 1, 194, 131, 70, 7, 146, 211, 20, 85, 18, 83, 22, 87, 210, 147, 84, 21, 82, 19, 86, 23, 42, 35, 40, 33, 170, 163, 46, 39, 107, 10, 105, 8, 235, 138, 111, 14, 186, 179, 60, 53, 58, 51, 62, 55, 251, 154, 125, 28, 123, 26, 127, 30, 98, 99, 96, 97, 226, 227, 102, 103, 106, 74, 104, 72, 234, 202, 110, 78, 242, 243, 116, 117, 114, 115, 118, 119, 250, 218, 124, 92, 122, 90, 126, 94, 180, 189, 36, 45, 182, 191, 166, 175, 221, 157, 77, 13, 223, 159, 207, 143, 176, 185, 48, 57, 160, 169, 164, 173, 217, 153, 89, 25, 201, 137, 205, 141, 148, 213, 4, 69, 150, 215, 134, 199, 212, 149, 68, 5, 214, 151, 198, 135, 144, 209, 16, 81, 128, 193, 132, 197, 208, 145, 80, 17, 192, 129, 196, 133, 188, 181, 44, 37, 190, 183, 174, 167, 253, 156, 109, 12, 255, 158, 239, 142, 184, 177, 56, 49, 168, 161, 172, 165, 249, 152, 121, 24, 233, 136, 237, 140, 244, 245, 100, 101, 246, 247, 230, 231, 252, 220, 108, 76, 254, 222, 238, 206, 240, 241, 112, 113, 224, 225, 228, 229, 248, 216, 120, 88, 232, 200, 236, 204]]
def xor(a, b):
for i in range(len(a)):
a[i] ^= b[i]
def fn1(round_no, state):
for i in range(16):
state[i] = FX[round_no%4][state[i]]
def un1(round_no, state):
for i in range(16):
for j in range(256):
if state[i] == FX[round_no%4][j]:
state[i] = j
break
def enc(pt, key):
state = pt[:]
xor(state, key)
for i in range(19):
fn1(i, state)
fn2(state)
fn3(state)
fn4(i, state, key)
fn1(19, state)
xor(state, key)
return state
def dec(ct, key):
state = ct[:]
xor(state, key)
un1(19, state)
for i in range(19)[::-1]:
fn4(i, state, key)
fn3(state)
un2(state)
un1(i, state)
xor(state, key)
return state