diff options
Diffstat (limited to '')
-rw-r--r-- | freestyle_hid/_freestyle_encryption.py | 67 |
1 files changed, 45 insertions, 22 deletions
diff --git a/freestyle_hid/_freestyle_encryption.py b/freestyle_hid/_freestyle_encryption.py index 0461cb8..a0268eb 100644 --- a/freestyle_hid/_freestyle_encryption.py +++ b/freestyle_hid/_freestyle_encryption.py @@ -6,7 +6,7 @@ class SpeckEncrypt: def __init__(self, key): # Perform key expansion and store the round keys - self.key = key & ((2 ** 128) - 1) + self.key = key & ((2**128) - 1) self.key_schedule = [self.key & 0xFFFFFFFF] key_buf = [(self.key >> (x * 32)) & 0xFFFFFFFF for x in range(1, 4)] for x in range(26): @@ -58,13 +58,19 @@ class SpeckEncrypt: def encrypt(self, iv, plain): plain = bytearray(plain) input_length = len(plain) - plain.extend(bytes(b'\x00' * (8 - (input_length % 8)))) - iv = int.from_bytes(iv.to_bytes(8, byteorder='big'), byteorder='little', signed=False) + plain.extend(bytes(b"\x00" * (8 - (input_length % 8)))) + iv = int.from_bytes( + iv.to_bytes(8, byteorder="big"), byteorder="little", signed=False + ) output = bytearray() for i in range(len(plain) // 8): k = self.encrypt_block(iv) - res = k ^ int.from_bytes(plain[i*8:i*8+8], byteorder='little', signed=False) - output.extend(int.to_bytes(res, 8, byteorder='little', signed=False)) + slice_start = i * 8 + slice_end = slice_start + 8 + res = k ^ int.from_bytes( + plain[slice_start:slice_end], byteorder="little", signed=False + ) + output.extend(int.to_bytes(res, 8, byteorder="little", signed=False)) iv += 1 encrypted = output[:input_length] return bytes(encrypted) @@ -72,24 +78,30 @@ class SpeckEncrypt: def decrypt(self, iv, encrypted): return self.encrypt(iv, encrypted) -class SpeckCMAC: +class SpeckCMAC: def __init__(self, key): self.cipher = SpeckEncrypt(key) k0 = self.cipher.encrypt_block(0) - k0 = int.from_bytes(k0.to_bytes(8, byteorder='big'), byteorder='little', signed=False) + k0 = int.from_bytes( + k0.to_bytes(8, byteorder="big"), byteorder="little", signed=False + ) - k1 = (k0 << 1) & 0XFFFFFFFFFFFFFFFF - if (k0 >> 63 != 0): + k1 = (k0 << 1) & 0xFFFFFFFFFFFFFFFF + if k0 >> 63 != 0: k1 ^= 0x1B - k2 = (k1 << 1) & 0XFFFFFFFFFFFFFFFF - if (k1 >> 63 != 0): + k2 = (k1 << 1) & 0xFFFFFFFFFFFFFFFF + if k1 >> 63 != 0: k2 ^= 0x1B - k1 = int.from_bytes(k1.to_bytes(8, byteorder='big'), byteorder='little', signed=False) - k2 = int.from_bytes(k2.to_bytes(8, byteorder='big'), byteorder='little', signed=False) + k1 = int.from_bytes( + k1.to_bytes(8, byteorder="big"), byteorder="little", signed=False + ) + k2 = int.from_bytes( + k2.to_bytes(8, byteorder="big"), byteorder="little", signed=False + ) self.k1 = k1 self.k2 = k2 @@ -98,22 +110,33 @@ class SpeckCMAC: i = 0 data_len = len(data) - while (i < data_len): + while i < data_len: data_left = data_len - i - if (data_left == 8): - block = int.from_bytes(data[i:i+8], 'little') ^ self.k1 - elif (data_left < 8): - block = int.from_bytes(data[i:i+data_left] + b'\x80' + b'\x00'*(7-data_left), 'little') ^ self.k2 + slice_start = i + slice_end = slice_start + 8 + if data_left == 8: + block = int.from_bytes(data[slice_start:slice_end], "little") ^ self.k1 + elif data_left < 8: + slice_end = i + data_left + block = ( + int.from_bytes( + data[slice_start:slice_end] + + b"\x80" + + b"\x00" * (7 - data_left), + "little", + ) + ^ self.k2 + ) else: - block = int.from_bytes(data[i:i+8], 'little') + block = int.from_bytes(data[slice_start:slice_end], "little") c = self.cipher.encrypt_block(c ^ block) i += 8 return c def derive(self, label, context): - data = label + b'\x00' + context + b'\x80\x00' - d1 = self.sign(b'\x01' + data) - d2 = self.sign(b'\x02' + data) << 64 + data = label + b"\x00" + context + b"\x80\x00" + d1 = self.sign(b"\x01" + data) + d2 = self.sign(b"\x02" + data) << 64 return d1 | d2 |