def helper_test_inverse_linear_transformation_over_finite_field( self, _in: bytes, expected_out: bytes): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) state = algorithm._in_to_state(_in) algorithm._inv_linear_transformation_over_finite_field(state) out = algorithm._state_to_out(state) self.assertEqual(expected_out, out)
def test_inv_non_linear_bijective_mapping(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) _in = b'\x9A\x2B\x1E\xAC\x76\xEE\x89\x1B\x91\x4A\xCF\x17\x7C\x98\xDD\x3D' _expected_out = b'\x26\x61\x70\x7E\xAF\x4F\xC7\xFD\x9E\x74\x91\xF7\xFC\x9F\xBE\x13' state = algorithm._in_to_state(_in) algorithm._inv_non_linear_bijective_mapping(state) self.assertEqual(_expected_out, algorithm._state_to_out(state))
def test_round_key_expand_128(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) expected_kt = b'\x86\x2F\x1F\x65\x3B\x77\x5B\xA1\xD0\x5C\xBC\x2F\x38\xE2\xD8\x7D' actual_kt = algorithm._key_expand_kt(_default_key_128()) self.assertEqual(expected_kt, algorithm._state_to_out(actual_kt))
def test_left_circular_shift(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) _in = b'\x9A\x2B\x1E\xAC\x7C\x98\xDD\x3D\x91\x4A\xCF\x17\x76\xEE\x89\x1B' _expected_out = b'\x9A\x2B\x1E\xAC\x76\xEE\x89\x1B\x91\x4A\xCF\x17\x7C\x98\xDD\x3D' state = algorithm._in_to_state(_in) algorithm._left_circular_shift(state) self.assertEqual(_expected_out, algorithm._state_to_out(state))
def test_round_key_expand_256(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_256()) expected_kt = b'\x1F\x44\x77\x80\x2D\x36\x68\x59\x9A\x40\x15\x36\x52\x48\x2C\xBF' actual_kt = algorithm._key_expand_kt(_default_key_256()) self.assertEqual(expected_kt, algorithm._state_to_out(actual_kt))
def test_key_expansion_128_256(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_256()) expected_keys = [ b'\x57\xC8\x16\xEB\x3F\x7E\x12\xDE\xED\x2C\x6B\x56\xE6\xB5\xBE\x1A', b'\xDE\xED\x2C\x6B\x56\xE6\xB5\xBE\x1A\x57\xC8\x16\xEB\x3F\x7E\x12', b'\xD8\x06\x9A\x7D\x88\x9A\xCD\x80\xCD\x31\x84\x45\x6C\xCC\xAE\x6F', b'\x80\xCD\x31\x84\x45\x6C\xCC\xAE\x6F\xD8\x06\x9A\x7D\x88\x9A\xCD', b'\xC3\x61\xCC\x97\x35\x13\x41\x1A\x82\x32\x4D\x2B\x67\x42\xF3\xFE', b'\x1A\x82\x32\x4D\x2B\x67\x42\xF3\xFE\xC3\x61\xCC\x97\x35\x13\x41', b'\x83\x10\x69\x8C\x65\xCF\x80\xA4\x09\xEF\x6F\xAA\xBE\xB8\x0F\x56', b'\xA4\x09\xEF\x6F\xAA\xBE\xB8\x0F\x56\x83\x10\x69\x8C\x65\xCF\x80', b'\xC6\xD5\xC4\xC3\x81\x46\x1A\x7B\x03\x4D\x69\x18\x42\x90\x15\x10', b'\x7B\x03\x4D\x69\x18\x42\x90\x15\x10\xC6\xD5\xC4\xC3\x81\x46\x1A', b'\x84\xD0\xF8\x21\x46\xC8\xBD\xF9\xB2\xB3\x70\x7B\x4D\x49\x38\x7E', b'\xF9\xB2\xB3\x70\x7B\x4D\x49\x38\x7E\x84\xD0\xF8\x21\x46\xC8\xBD', b'\x43\xFA\xDB\x28\xA0\xD1\xD4\x2B\xBF\xF9\x2F\xF9\x79\x45\x46\xB3', b'\x2B\xBF\xF9\x2F\xF9\x79\x45\x46\xB3\x43\xFA\xDB\x28\xA0\xD1\xD4', b'\xC3\x1E\xE8\xA8\x7E\x2C\xED\x24\x5A\x21\xA4\x35\xFD\xB2\x5B\x92' ] self.assertEqual(len(expected_keys), len(algorithm._round_keys)) for key_index in range(len(expected_keys)): print(f'key_index: {key_index}') expected_key = kalyna._KalynaEncryptor._in_to_state( expected_keys[key_index]) actual_key = algorithm._round_keys[key_index] if len(expected_key[0]) > 0: self.assertEqual(expected_key, actual_key)
def helper_test_add_round_key_modulo_2(self, state_bytes: bytes, key_bytes: bytes, expected_out: bytes): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) state = algorithm._in_to_state(state_bytes) key = algorithm._in_to_state(key_bytes) algorithm._add_round_key_modulo_2(state, key) self.assertEqual(expected_out, algorithm._state_to_out(state))
def test_in_to_state(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) _in = b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' expected_state = [[0x10, 0x18], [0x11, 0x19], [0x12, 0x1A], [0x13, 0x1B], [0x14, 0x1C], [0x15, 0x1D], [0x16, 0x1E], [0x17, 0x1F]] self.assertEqual(expected_state, algorithm._in_to_state(_in))
def test_rotate_left(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) _in = b'\x16\x50\x5e\x6b\x9b\x3a\xb1\xe6\x86\x5b\x77\xdc\xe0\x82\xa0\xf4' expected_out = b'\xe6\x86\x5b\x77\xdc\xe0\x82\xa0\xf4\x16\x50\x5e\x6b\x9b\x3a\xb1' state = algorithm._in_to_state(_in) out_state = algorithm._in_to_state(expected_out) algorithm._rotate_left(2, state) self.assertEqual(out_state, state)
def helper_test_subtract_round_key_modulo_2_64(self, state_bytes: bytes, round_key_bytes: bytes, expected_out_bytes: bytes): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) state = algorithm._in_to_state(state_bytes) round_key = algorithm._in_to_state(round_key_bytes) algorithm._subtract_round_key_modulo_2_64(state, round_key) self.assertEqual(expected_out_bytes, algorithm._state_to_out(state))
def test_key_expansion_128_128(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) expected_keys = [ b'\x16\x50\x5e\x6b\x9b\x3a\xb1\xe6\x86\x5b\x77\xdc\xe0\x82\xa0\xf4', b'\xE6\x86\x5B\x77\xDC\xE0\x82\xA0\xF4\x16\x50\x5E\x6B\x9B\x3A\xB1', b'\x7E\x70\x87\x6E\xAE\x49\x84\x76\x8A\xAA\xA0\x0A\x7C\x93\xEC\x42', b'\x76\x8A\xAA\xA0\x0A\x7C\x93\xEC\x42\x7E\x70\x87\x6E\xAE\x49\x84', b'\x45\xCE\xD4\xC5\x1E\x91\x40\xF5\x3E\x72\x76\x82\x0F\x0B\xD9\xFE', b'\xF5\x3E\x72\x76\x82\x0F\x0B\xD9\xFE\x45\xCE\xD4\xC5\x1E\x91\x40', b'\x8C\x77\xEE\x22\x79\x00\xC4\x62\x51\x5F\x66\x32\x05\x60\xC4\xB1', b'\x62\x51\x5F\x66\x32\x05\x60\xC4\xB1\x8C\x77\xEE\x22\x79\x00\xC4', b'\x0A\x98\x72\xE2\x5C\xD2\xB0\xB8\xAA\x87\x9A\x20\x86\xA6\x6D\xD8', b'\xB8\xAA\x87\x9A\x20\x86\xA6\x6D\xD8\x0A\x98\x72\xE2\x5C\xD2\xB0', b'\x57\x26\xB1\xA8\x94\xDB\xC4\x18\xF6\x0B\xF3\xD5\xE8\xD7\x48\x61' ] self.assertEqual(len(expected_keys), len(algorithm._round_keys)) for key_index in range(len(expected_keys)): expected_key = kalyna._KalynaEncryptor._in_to_state( expected_keys[key_index]) actual_key = algorithm._round_keys[key_index] if len(expected_key[0]) > 0: self.assertEqual(expected_key, actual_key)
def test_cipher_inv_cipher(self): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) _in = b'S***n krasavchik' self.assertEqual(_in, algorithm.inv_cipher(algorithm.cipher(_in)))
def helper_test_kalyna_inv_cipher(self, key: bytes, _in: bytes, expected_out: bytes): algorithm = kalyna._KalynaEncryptor(128, key) self.assertEqual(expected_out, algorithm.inv_cipher(_in))
def helper_test_kalyna_cipher(self, _in, expected_out): algorithm = kalyna._KalynaEncryptor(128, _default_key_128()) self.assertEqual(expected_out, algorithm.cipher(_in))