cube attack black box

This commit is contained in:
2026-07-09 16:25:29 +02:00
parent baa9bdb16d
commit 6bbbd2fec8
4 changed files with 363 additions and 11 deletions
+99 -7
View File
@@ -1,12 +1,13 @@
from tea3.pretty_print import pretty_print
from tea3.cliutils import prompt_int, prompt_choice, prompt_list
from tea3.tea3model import Tea3Model
from tea3.utils import set_bits
from tea3.utils import set_bits, set_int_bits
from tea3.variable_search import run_exhaustive, run_exhaustive_staircase, run_exhaustive_staircase2, run_exhaustive_bp
from tea3.sbox import run_sbox
from tea3.variable_xor import run_variable_xor, run_exhaustive_xor
from tea3.f31f32 import run_f31f32
from tea3.cube_attack import run_cube_attack
from tea3.cube_attack_blackbox import run_cube_attack_offline_tea3
from tea3.precompute import run_precompute_cli, run_load_cli
@@ -200,6 +201,94 @@ def run_cube_attack_cli(model = None):
print("Done.")
def run_cube_attack_oracle_cli():
print("\nBlack-box cube attack search on TEA3.")
print("This mode varies frame-number bits and queries the cipher directly.")
print("Frame-number bits are indexed 031, with 0 = least significant bit.")
base_frame_number = prompt_int("Base frame number (04294967295): ", 0, 0xFFFFFFFF)
raw = input("Frame bits to set to 0 (space-separated): ").strip()
fixed_zero_bits = raw.split() if raw else []
raw = input("Frame bits to set to 1 (space-separated): ").strip()
fixed_one_bits = raw.split() if raw else []
output_byte = prompt_int("Output keystream byte (063): ", 0, 63)
output_bit = prompt_int("Output bit within that byte (07): ", 0, 7)
cube_size = prompt_int("Cube size (132): ", 1, 32)
print("\nSearch strategy:")
print(" 1) Exhaustive")
print(" 2) Random sampling")
strategy = prompt_choice("Your choice (1 or 2): ", {1, 2})
if strategy == 1:
mode = "exhaustive"
samples = 0
limit = prompt_int("How many results to keep? (1100): ", 1, 100)
else:
mode = "random"
samples = prompt_int("How many random cubes to test? (1200000): ", 1, 200000)
limit = prompt_int("How many results to keep? (1100): ", 1, 100)
raw = input(
"Enter 10 key bytes in hex or decimal, separated by spaces "
"(leave empty for all-zero key): "
).strip()
if raw:
try:
key_register = [int(x, 0) & 0xFF for x in raw.split()]
except ValueError as e:
print(f"Error: invalid key byte: {e}")
return
if len(key_register) != 10:
print("Error: TEA3 key register must contain exactly 10 bytes.")
return
else:
key_register = [0] * 10
try:
fixed_bits = {}
for b in fixed_zero_bits:
fixed_bits[int(b)] = 0
for b in fixed_one_bits:
fixed_bits[int(b)] = 1
except ValueError:
print("Error: frame bit indices must be integers.")
return
public_bits = [i for i in range(32) if i not in fixed_bits]
if not public_bits:
print("Error: no unfixed frame bits left to use as public variables.")
return
base_frame_number = set_int_bits(base_frame_number, fixed_zero_bits, 0)
base_frame_number = set_int_bits(base_frame_number, fixed_one_bits, 1)
print("-" * 50)
try:
run_cube_attack_offline_tea3(
key_register=key_register,
base_frame_number=base_frame_number,
public_bits=public_bits,
cube_size=cube_size,
mode=mode,
samples=samples,
limit=limit,
output_byte=output_byte,
output_bit=output_bit,
)
except ValueError as e:
print(f"Error: {e}")
print("\n" + "=" * 50)
print("Done.")
def main():
model = None
@@ -216,14 +305,15 @@ def main():
print(" 5) Variable XOR")
print(" 6) F31, F32 analysis")
print(" 7) Exhaustive XOR search")
print(" 8) Cube attack search")
print(" 9) Precompute and save model")
print(" 10) Load model")
print(" 8) Cube attack search (symbolic)")
print(" 9) Cube attack search (oracle)")
print(" 10) Precompute and save model")
print(" 11) Load model")
print(" 0) Exit")
mode = prompt_choice(
"Your choice (0-10): ",
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
"Your choice (0-11): ",
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
)
if mode == 0:
@@ -246,8 +336,10 @@ def main():
elif mode == 8:
run_cube_attack_cli(model)
elif mode == 9:
run_precompute_cli()
run_cube_attack_oracle_cli()
elif mode == 10:
run_precompute_cli()
elif mode == 11:
model = run_load_cli()
input("\nPress Enter to return to the main menu...")