Beispiel #1
0
class GsCipher:
    gs_encryption_key = None
    gs_iv = None
    gs_speck = None
    mode = None
    logger = None
    key_int = None
    iv_int = None

    def __init__(self):
        key_bytes = array.array('B', [])
        for k in self.gs_encryption_key:
            key_bytes.append(k)
        self.key_int = int.from_bytes(key_bytes,
                                      byteorder='little',
                                      signed=False)
        iv_bytes = array.array('B', [])
        for i in self.gs_iv:
            iv_bytes.append(i)
        self.iv_int = int.from_bytes(iv_bytes,
                                     byteorder='little',
                                     signed=False)
        self.gs_speck = SpeckCipher(self.key_int,
                                    key_size=128,
                                    block_size=64,
                                    mode=self.mode,
                                    init=self.iv_int)

    def encrypt(self, ax25_packet):
        if self.gs_speck.mode == 'CBC':
            self.gs_speck.update_iv(self.iv_int)
        ax25_packet_encrypted = array.array('B', ax25_packet[:16])
        ax25_packet_temp = array.array('B', ax25_packet[16:])
        for i in range(0, 232, 8):
            plaintext_int = int.from_bytes(ax25_packet_temp[i:(i + 8)],
                                           byteorder='big',
                                           signed=False)
            ciphertext = self.gs_speck.encrypt(plaintext_int)
            ciphertext_bytes = bytearray.fromhex('{:032x}'.format(ciphertext))
            for c in ciphertext_bytes[8:]:
                ax25_packet_encrypted.append(c)
        ax25_packet_encrypted.extend([0x00] * 5)
        return ax25_packet_encrypted

    def decrypt(self, ax25_packet_encrypted):
        if self.gs_speck.mode == 'CBC':
            self.gs_speck.update_iv(self.iv_int)
        ax25_packet = array.array('B', ax25_packet_encrypted[:16])
        ax25_packet_temp = ax25_packet_encrypted[16:]
        for i in range(0, 232, 8):
            ciphertext_int = int.from_bytes(ax25_packet_temp[i:(i + 8)],
                                            byteorder='big',
                                            signed=False)
            plaintext = self.gs_speck.decrypt(ciphertext_int)
            plaintext_bytes = bytearray.fromhex('{:032x}'.format(plaintext))
            for p in plaintext_bytes[8:]:
                ax25_packet.append(p)
        return ax25_packet