Created
November 20, 2022 11:18
-
-
Save viewv/cd197424b50df1d71e526ea1d427c2d6 to your computer and use it in GitHub Desktop.
MixColumns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PD = [ | |
[0x02, 0x03, 0x01, 0x01], | |
[0x01, 0x02, 0x03, 0x01], | |
[0x01, 0x01, 0x02, 0x03], | |
[0x03, 0x01, 0x01, 0x02], | |
] | |
def gf3mpy(x, y): | |
p = 0b100011011 | |
m = 0 | |
for _ in range(8): | |
m = m << 1 | |
if m & 0b100000000: | |
m = m ^ p | |
if y & 0b010000000: | |
m = m ^ x | |
y = y << 1 | |
return m | |
def main(state): | |
result = [] | |
for i in range(4): | |
r = [] | |
p = PD[i] | |
for j in range(4): | |
s = [state[_][j] for _ in range(4)] | |
temp = 0x00 | |
for k in range(4): | |
x, y = p[k], s[k] | |
temp ^= gf3mpy(x,y) | |
r.append(temp) | |
result.append(r) | |
return result | |
if __name__=="__main__": | |
state = [ | |
[0x33, 0xD2, 0x04,0x96], | |
[0xF1, 0xC4, 0xD0,0x0C], | |
[0x8C, 0xE7, 0xDE,0x48], | |
[0xD3, 0xA4, 0x2C,0xB5], | |
] | |
answer = main(state) | |
for x in answer: | |
for y in x: | |
print(hex(y), end=',') | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment