More descriptive error msg when laf_crypto fails to import, py2 compat, pytest for crypto

This commit is contained in:
tuxuser 2018-02-05 15:50:51 +01:00
parent cef3647a87
commit eb25f61760
4 changed files with 46 additions and 14 deletions

View file

@ -1,11 +1,16 @@
from Crypto.Cipher import AES
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''
for x in range(32, 0, -1):
new_key += int_as_byte(old_key[x-1] - (x % 0x0C))
c = old_key[x-1]
if not isinstance(c, int):
c = ord(c)
new_key += int_as_byte(c - (x % 0x0C))
return new_key
@ -13,11 +18,10 @@ 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):
key_xor += int_as_byte(key[pos] ^ kilo_challenge[3])
key_xor += int_as_byte(key[pos + 1] ^ kilo_challenge[2])
key_xor += int_as_byte(key[pos + 2] ^ kilo_challenge[1])
key_xor += int_as_byte(key[pos + 3] ^ kilo_challenge[0])
k = struct.unpack('<I', key[pos:pos + 4])[0]
key_xor += struct.pack('<I', k ^ challenge)
pos += 4
return key_xor
@ -29,5 +33,6 @@ def encrypt_kilo_challenge(encryption_key, kilo_challenge):
plaintext += int_as_byte(k)
encryption_key = key_transform(encryption_key)
xored_key = xor_key(encryption_key, kilo_challenge)
obj = AES.new(xored_key, AES.MODE_ECB)
return obj.encrypt(plaintext)
obj = Cipher(algorithms.AES(xored_key), modes.ECB(),
backend=default_backend()).encryptor()
return obj.update(plaintext) + obj.finalize()

View file

@ -24,16 +24,21 @@ except ImportError:
_logger = logging.getLogger("LGLAF.py")
# Python 2/3 compat
try: input = raw_input
except: pass
if '\0' == b'\0': int_as_byte = chr
else: int_as_byte = lambda x: bytes([x])
try:
input = raw_input
except:
pass
if '\0' == b'\0':
int_as_byte = chr
else:
int_as_byte = lambda x: bytes([x])
# laf crypto for KILO challenge/response
try:
import laf_crypto
except ImportError:
_logger.warning("LAF Crypto failed to import!")
except ImportError as e:
_logger.warning("LAF Crypto failed to import! Error: %s" % e)
pass
# Use Manufacturer key for KILO challenge/response

5
tests/conftest.py Normal file
View file

@ -0,0 +1,5 @@
import pytest
@pytest.fixture(scope='session')
def laf_key():
return b'qndiakxxuiemdklseqid~a~niq,zjuxl'

17
tests/test_crypto.py Normal file
View file

@ -0,0 +1,17 @@
from binascii import unhexlify
import laf_crypto
def test_transform(laf_key):
transformed = laf_crypto.key_transform(laf_key)
assert transformed == b'dqoev)ohnsWu\\bk`oiicmZ_lpqe\\ealp'
def test_xor_key(laf_key):
transformed_key = b'dqoev)ohnsWu\\bk`oiicmZ_lpqe\\ealp'
challenge = unhexlify(b'f29ae130')
xored_key = laf_crypto.xor_key(transformed_key, challenge)
assert xored_key == b'T\x90\xf5\x97F\xc8\xf5\x9a^\x92\xcd\x87l\x83\xf1\x92_\x88\xf3\x91]\xbb\xc5\x9e@\x90\xff\xaeU\x80\xf6\x82'
def test_challenge(laf_key):
resp = laf_crypto.encrypt_kilo_challenge(laf_key, unhexlify(b'f29ae130'))
assert resp == unhexlify(b'2f47ca81ebeee6f414263c0542c8d132')