def test_get_all_possible_keys(normal_state: bytes, faulty_state: bytes, key: bytes) -> None: """Assert that key[12], k[9], k[6] and k[3] are in the hypothesis.""" last_key = b"".join(key_expension(key, 11)[-4:]) equations = get_all_possible_keys(_get_state(normal_state), _get_state(faulty_state)) found = False for eq in equations: if all(last_key[i] in eq[i + 1] for i in range(0x10)): found = True assert found
def test_compute_second_column(normal_state: bytes, faulty_state: bytes, key: bytes) -> None: """Assert that key[4], k[1], k[14] and k[11] are in the hypothesis.""" last_key = b"".join(key_expension(key, 11)[-4:]) equations = _compute_second_column(_get_state(normal_state), _get_state(faulty_state)) found = False for d, eq in equations.items(): if last_key[4] in eq[5] and last_key[1] in eq[2] and last_key[ 14] in eq[15] and last_key[11] in eq[12]: found = True assert found
def test_compute_fourth_column(normal_state: bytes, faulty_state: bytes, key: bytes) -> None: """Assert that key[12], k[9], k[6] and k[3] are in the hypothesis.""" last_key = b"".join(key_expension(key, 11)[-4:]) equations = _compute_fourth_column(_get_state(normal_state), _get_state(faulty_state)) found = False for d, eq in equations.items(): if last_key[12] in eq[13] and last_key[9] in eq[10] and last_key[ 6] in eq[7] and last_key[3] in eq[4]: found = True assert found
def test_key_expension(): key = binascii.unhexlify("2b7e151628aed2a6abf7158809cf4f3c") words = key_expension(key, rounds=11) generated_keys = [ b"".join(words[i:i + 4]) for i in range(0, len(words), 4) ] assert generated_keys == [ binascii.unhexlify(k) for k in [ "2b7e151628aed2a6abf7158809cf4f3c", "a0fafe1788542cb123a339392a6c7605", "f2c295f27a96b9435935807a7359f67f", "3d80477d4716fe3e1e237e446d7a883b", "ef44a541a8525b7fb671253bdb0bad00", "d4d1c6f87c839d87caf2b8bc11f915bc", "6d88a37a110b3efddbf98641ca0093fd", "4e54f70e5f5fc9f384a64fb24ea6dc4f", "ead27321b58dbad2312bf5607f8d292f", "ac7766f319fadc2128d12941575c006e", "d014f9a8c9ee2589e13f0cc8b6630ca6", ] ]
def test_is_valid_guess(key: bytes, expected: bool) -> None: last_key = b"".join(key_expension(key, 11)[-4:]) normal_state = _get_state(b"\x81\xd6\xcd\xc3\xbd\x16\xfb\x8dr\xb9\xbb\x88\x81\x8b[\xe9") faulty_state = _get_state(b"\xef\xf95\x08c\x01\x87\xb8\xd3IN\x8bp\xe6\x88~") assert _is_valid_guess(normal_state, faulty_state, last_key) == (last_key, expected)