def test_all_vecs(self): for key, nonce, data, size, plaintext, expected_ciphertext in TEST_VECTORS: key = Bytes(key) nonce = Bytes(nonce) data = Bytes(data) data = data.zfill(len(data) + 1) plaintext = Bytes(plaintext) ccm = CCM(Rijndael(key), size) ciphertext = ccm.encrypt(nonce, plaintext, data) self.assertEqual(ciphertext, Bytes(expected_ciphertext)) self.assertEqual(ccm.decrypt(nonce, ciphertext, data), plaintext)
def generate(self, length: int) -> Bytes: """ Generates `length` of keystream. Parameters: length (int): Desired length of keystream in bytes. Returns: Bytes: Keystream. """ keystream = Bytes(b'') for _ in range(math.ceil(length / 4)): self.reorganize_bits() keystream += Bytes(self.F() ^ self.X[3]) self.run_lfsr() return keystream.zfill(length)[:length]