Skip to content

Instantly share code, notes, and snippets.

@dfyz
Created November 8, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfyz/d96db25c3a207914ab3f85d9309757dd to your computer and use it in GitHub Desktop.
Save dfyz/d96db25c3a207914ab3f85d9309757dd to your computer and use it in GitHub Desktop.
import galois
from itertools import chain
import numpy as np
class SymbolicByte:
def __init__(self, bit_vars):
self.bit_vars = [
self._xor_vars(vs) for vs in bit_vars
]
@staticmethod
def from_const(const):
return SymbolicByte(
[['1'] if (const & (1 << bit_idx)) else []]
for bit_idx in range(8)
)
def _xor_vars(self, all_vars):
res = set()
for v in chain.from_iterable(all_vars):
if v in res:
res.remove(v)
else:
res.add(v)
return res
def __xor__(self, other):
if isinstance(other, int):
return self ^ SymbolicByte.from_const(other)
return SymbolicByte(zip(self.bit_vars, other.bit_vars))
def eval(self, var_dict):
res = 0
for bit_idx in range(8):
bit_val = 0
for var_name in self.bit_vars[bit_idx]:
bit_val ^= (1 if var_name == '1' else var_dict[var_name])
if bit_val:
res |= 1 << bit_idx
return res
def dot(mask, num):
res = 0
while num:
if mask & 1:
res ^= num & 1
mask >>= 1
num >>= 1
return res
class AffineSBox:
def __init__(self, perm):
assert len(set(perm)) == 256, 'Not a permutation'
self.add_indexes = []
self.add_const = []
for out_bit in range(8):
for in_mask in range(256):
dots = {dot(in_mask, b) ^ dot(1 << out_bit, perm[b]) for b in range(256)}
if len(dots) == 1:
# To get the output bit, we should add together the bits with indexes from in_mask...
self.add_indexes.append([idx for idx in range(8) if in_mask & (1 << idx)])
# ...and add 1 if the output bit always equals to the inverse of the sum of the input bits.
self.add_const.append(next(iter(dots)) == 1)
break
else:
raise Exception(f'Output bit #{out_bit} is not an affine function of input bits')
def __getitem__(self, byte):
return SymbolicByte([
[byte.bit_vars[idx] for idx in indexes] + [{'1'} if const else set()]
for indexes, const in zip(self.add_indexes, self.add_const)
])
class AES:
sbox = AffineSBox((
0x4c, 0x51, 0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c, 0x79, 0x7a,
0x3b, 0x38, 0x3d, 0x3e, 0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a,
0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16, 0x1f, 0x1c, 0x19, 0x1a
))
rcon = (0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36)
def __init__(self, key):
self._block_size = 16
self._round_keys = self._expand_key([i for i in key])
self._state = []
def _transpose(self, m):
return [m[4 * j + i] for i in range(4) for j in range(4)]
def _xor(self, a, b):
return [x ^ y for x, y in zip(a, b)]
def _expand_key(self, key):
round_keys = [key]
for i in range(10):
round_key = []
first = round_keys[i][:4]
last = round_keys[i][-4:]
last = last[1:] + [last[0]]
last = [self.sbox[i] for i in last]
round_key.extend(self._xor(self._xor(first, last), [self.rcon[i+1], 0, 0, 0]))
for j in range(0, 12, 4):
round_key.extend(self._xor(round_key[j:j + 4], round_keys[i][j + 4:j + 8]))
round_keys.append(round_key)
for i in range(len(round_keys)):
round_keys[i] = self._transpose(round_keys[i])
return round_keys
def _add_round_key(self, i):
self._state = self._xor(self._round_keys[i], self._state)
def _mix_columns(self):
s = [0] * self._block_size
for i in range(4):
s[i] = self.gmul2[self._state[i]] ^ self.gmul3[self._state[i + 4]] ^ self._state[i + 8] ^ self._state[i + 12]
s[i + 4] = self._state[i] ^ self.gmul2[self._state[i + 4]] ^ self.gmul3[self._state[i + 8]] ^ self._state[i + 12]
s[i + 8] = self._state[i] ^ self._state[i + 4] ^ self.gmul2[self._state[i + 8]] ^ self.gmul3[self._state[i + 12]]
s[i + 12] = self.gmul3[self._state[i]] ^ self._state[i + 4] ^ self._state[i + 8] ^ self.gmul2[self._state[i + 12]]
self._state = s
def _shift_rows(self):
self._state = [
self._state[0], self._state[1], self._state[2], self._state[3],
self._state[5], self._state[6], self._state[7], self._state[4],
self._state[10], self._state[11], self._state[8], self._state[9],
self._state[15], self._state[12], self._state[13], self._state[14]
]
def _sub_bytes(self):
self._state = [self.sbox[i] for i in self._state]
def _encrypt_block(self):
self._add_round_key(0)
for i in range(1, 10):
self._sub_bytes()
self._shift_rows()
self._mix_columns()
self._add_round_key(i)
self._sub_bytes()
self._shift_rows()
self._add_round_key(10)
def encrypt(self, block):
self._state = self._transpose(block)
self._encrypt_block()
return self._transpose(self._state)
if __name__ == '__main__':
def symbolic_block(block_name):
return [
SymbolicByte(
[[f'{block_name}{byte_idx}_{bit_idx}']]
for bit_idx in range(8)
)
for byte_idx in range(16)
]
aes = AES(symbolic_block('K'))
target_bytes = b'codegate2022{xx}'
encrypted0_m = aes.encrypt(target_bytes)
GF2 = galois.GF(2)
mat = []
bias = []
target = []
for byte_idx in range(16):
for bit_idx in range(8):
set_vars = set(encrypted0_m[byte_idx].bit_vars[bit_idx])
cur_row = []
for key_byte_idx in range(16):
for key_bit_idx in range(8):
key_var_name = f'K{key_byte_idx}_{key_bit_idx}'
cur_row.append(1 if key_var_name in set_vars else 0)
mat.append(cur_row)
bias.append(1 if '1' in set_vars else 0)
target.append(1 if (target_bytes[byte_idx] & (1 << bit_idx)) else 0)
mat = GF2(mat)
bias = GF2(bias)
target = GF2(target)
key_sln = np.linalg.solve(mat, target - bias)
key_bytes = bytearray()
for byte_idx in range(16):
cur_num = 0
for bit_idx in range(8):
if key_sln[byte_idx * 8 + bit_idx] == 1:
cur_num |= (1 << bit_idx)
key_bytes.append(cur_num)
print(key_bytes.hex())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment