-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAES_core.py
54 lines (48 loc) · 1.94 KB
/
AES_core.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
def sub_bytes(state):
"""
SubBytes step in AES. Non-linear substitution step where each byte is replaced with another according to a lookup table.
For simplicity, using a dummy S-box which just returns the byte itself (no real substitution).
"""
return state # Placeholder for the actual substitution using S-box.
def shift_rows(state):
"""
ShiftRows step in AES. Cyclically shift the bytes in each row to the left; different shifts for each row.
For simplicity, assuming the state is a list of four rows, each row is also a list of four bytes.
"""
state[1] = state[1][1:] + state[1][:1] # Row 1: Shift left by 1
state[2] = state[2][2:] + state[2][:2] # Row 2: Shift left by 2
state[3] = state[3][3:] + state[3][:3] # Row 3: Shift left by 3
return state
def mix_columns(state):
"""
MixColumns step in AES. Columns are mixed using a fixed polynomial represented as a matrix.
For simplicity, this function performs a dummy operation which just returns the state unchanged.
"""
return state # Placeholder for the actual MixColumns operation.
def add_round_key(state, round_key):
"""
AddRoundKey step in AES. Each byte of the state is combined with the round key using bitwise XOR.
"""
for i in range(4):
for j in range(4):
state[i][j] ^= round_key[i][j]
return state
# Example state and round key for testing the functions
state_example = [
[0x32, 0x88, 0x31, 0xe0],
[0x43, 0x5a, 0x31, 0x37],
[0xf6, 0x30, 0x98, 0x07],
[0xa8, 0x8d, 0xa2, 0x34]
]
round_key_example = [
[0x2b, 0x28, 0xab, 0x09],
[0x7e, 0xae, 0xf7, 0xcf],
[0x15, 0xd2, 0x15, 0x4f],
[0x16, 0xa6, 0x88, 0x3c]
]
# Apply each function
state_subbed = sub_bytes(state_example)
state_shifted = shift_rows(state_subbed)
state_mixed = mix_columns(state_shifted)
state_rounded = add_round_key(state_mixed, round_key_example)
state_rounded # Output the final state after one round