forked from Lekensteyn/lglaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laf_crypto.py
37 lines (31 loc) · 1.13 KB
/
laf_crypto.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
import struct
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from lglaf import int_as_byte
def key_transform(old_key):
new_key = b''
old_key = bytearray(old_key)
for x in range(32, 0, -1):
c = old_key[x-1]
new_key += int_as_byte(c - (x % 0x0C))
return new_key
def xor_key(key, kilo_challenge):
# Reserve key
key_xor = b''
pos = 0
challenge = struct.unpack('>I', kilo_challenge)[0]
for i in range(8):
k = struct.unpack('<I', key[pos:pos + 4])[0]
key_xor += struct.pack('<I', k ^ challenge)
pos += 4
return key_xor
def encrypt_kilo_challenge(encryption_key, kilo_challenge):
plaintext = b''
for k in range(0, 16):
# Assemble 0x00 0x01 0x02 ... 0x1F byte-array
plaintext += int_as_byte(k)
encryption_key = key_transform(encryption_key)
xored_key = xor_key(encryption_key, kilo_challenge)
obj = Cipher(algorithms.AES(xored_key), modes.ECB(),
backend=default_backend()).encryptor()
return obj.update(plaintext) + obj.finalize()