messing around

This commit is contained in:
Thomas Faour 2025-11-15 14:51:09 -05:00
parent ab942a1bbc
commit b1d2511b83
3 changed files with 57 additions and 24 deletions

View File

@ -1,3 +1,10 @@
# multilingual_aes
Implementing AES in every language I can!
Implementing AES in every language I can!
## Project Goals
- Provide a clear, idiomatic AES implementation in multiple languages.
- Keep APIs and test vectors consistent across implementations so outputs are comparable.

3
python/src/from src.py Normal file
View File

@ -0,0 +1,3 @@
from src.utility import mix_columns
state = b'\xDB\x13\x53\x45\xDB\x13\x53\x45\xDB\x13\x53\x45\xDB\x13\x53\x45'
mix_columns(state)

View File

@ -17,6 +17,7 @@ _SBOX = [
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
]
def s_box(byte: int) -> int:
"""
Substitutes a single byte using the AES S-Box.
@ -46,28 +47,6 @@ def shift_rows(state: bytes) -> bytes:
rot_bytes(state[i:i+4], i//4) for i in range(0, 16, 4)
])
_polynomial = [0x02, 0x03, 0x01, 0x01]
_mix_cols_mat = [
_polynomial,
rot_bytes(_polynomial, 1),
rot_bytes(_polynomial, 2),
rot_bytes(_polynomial, 3),
]
def mix_columns(state: bytes) -> bytes:
"""
Performs the MixColumns transformation on the state.
Args:
state (bytes): The state to be transformed, expected to be 16 bytes long.
Returns:
bytes: The transformed state after applying MixColumns.
Raises:
ValueError: If the input state is not 16 bytes long.
"""
for i in range(4):
b = state[i]
newb = state[i]
def rot_bytes(word: bytes, n_rots: int = 1) -> bytes:
"""
@ -95,7 +74,51 @@ def xor_bytes(a: bytes, b: bytes) -> bytes:
if len(a) != len(b):
raise ValueError("Byte arrays must be of the same length")
return bytes(x ^ y for x, y in zip(a, b))
return
_polynomial = [0x02, 0x03, 0x01, 0x01]
_mix_cols_mat = [
_polynomial,
rot_bytes(_polynomial, 1),
rot_bytes(_polynomial, 2),
rot_bytes(_polynomial, 3),
]
def _get_col(a: bytes, i: int) -> bytes:
return bytes(a[i + j * 4] for j in range(4))
def _xor_dot_product(a: bytes, b: bytes) -> int:
print(f"Multiplying {a.hex()} and {b.hex()}")
prod = [a[i]*b[i] for i in range(len(a))]
result = prod[0]
for i in range(len(prod)-1):
if prod[i+1] > 0xff:
prod[i+1] ^= 0x11b
result ^= prod[i+1]
return result%256
def mix_columns(state: bytes) -> bytes:
"""
Performs the MixColumns transformation on the state.
Args:
state (bytes): The state to be transformed, expected to be 16 bytes long.
Returns:
bytes: The transformed state after applying MixColumns.
Raises:
ValueError: If the input state is not 16 bytes long.
"""
new_state = bytearray(16)
for i in range(0, 4):
for j in range(0,4):
new_state[j*4+i] = _xor_dot_product(
bytes(_mix_cols_mat[j]),
bytes(_get_col(state, i))
)
return bytes(new_state)
def get_rcon(n_rounds: int) -> list:
"""