From b1d2511b837f054702f80671d1ecde88de334bda Mon Sep 17 00:00:00 2001 From: Thomas Faour Date: Sat, 15 Nov 2025 14:51:09 -0500 Subject: [PATCH] messing around --- README.md | 9 +++++- python/src/from src.py | 3 ++ python/src/utility.py | 69 ++++++++++++++++++++++++++++-------------- 3 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 python/src/from src.py diff --git a/README.md b/README.md index e0a5d42..01b01b1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ # multilingual_aes -Implementing AES in every language I can! \ No newline at end of file +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. + + diff --git a/python/src/from src.py b/python/src/from src.py new file mode 100644 index 0000000..7677359 --- /dev/null +++ b/python/src/from src.py @@ -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) \ No newline at end of file diff --git a/python/src/utility.py b/python/src/utility.py index 59658a3..1bb7362 100644 --- a/python/src/utility.py +++ b/python/src/utility.py @@ -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: """