108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Host-side LUKS2 full-disk encryption image builder for Geminux."""
|
||
|
|
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import subprocess
|
||
|
|
|
||
|
|
LUKS2_HEADER_PAD_MIB = 32
|
||
|
|
DEFAULT_PBKDF_MEMORY_KB = 131072
|
||
|
|
DEFAULT_PBKDF_ITERATIONS = 4
|
||
|
|
DEFAULT_PBKDF_PARALLEL = 4
|
||
|
|
|
||
|
|
|
||
|
|
class LuksConfig:
|
||
|
|
"""LUKS2 encryption configuration collected before flashing."""
|
||
|
|
|
||
|
|
def __init__(self, enabled=False, passphrase=None):
|
||
|
|
self.enabled = bool(enabled)
|
||
|
|
self.passphrase = passphrase
|
||
|
|
|
||
|
|
|
||
|
|
def validate_luks_passphrase(passphrase):
|
||
|
|
"""Validate a LUKS2 disk passphrase (non-empty UTF-8 string without NUL/newlines)."""
|
||
|
|
if not isinstance(passphrase, str) or not passphrase:
|
||
|
|
raise ValueError("LUKS2 passphrase must be a non-empty string")
|
||
|
|
if any(ch in passphrase for ch in ("\x00", "\n", "\r")):
|
||
|
|
raise ValueError("LUKS2 passphrase must not contain NUL or newline characters")
|
||
|
|
return passphrase
|
||
|
|
|
||
|
|
|
||
|
|
def _run_cryptsetup(args, img_dir, input_bytes=None):
|
||
|
|
host_cs = shutil.which("cryptsetup")
|
||
|
|
if host_cs is not None:
|
||
|
|
return subprocess.run(
|
||
|
|
[host_cs] + args,
|
||
|
|
input=input_bytes,
|
||
|
|
check=True,
|
||
|
|
capture_output=True,
|
||
|
|
)
|
||
|
|
docker_bin = shutil.which("docker")
|
||
|
|
if docker_bin is None:
|
||
|
|
raise RuntimeError("Need host 'cryptsetup' or 'docker' to encrypt rootfs image")
|
||
|
|
return subprocess.run(
|
||
|
|
[
|
||
|
|
docker_bin, "run", "--rm", "-i",
|
||
|
|
"-v", f"{img_dir}:/img",
|
||
|
|
"alpine:3.24",
|
||
|
|
"sh", "-eu", "-c",
|
||
|
|
'apk add --no-cache cryptsetup >/dev/null && "$@"',
|
||
|
|
"_", "cryptsetup",
|
||
|
|
] + args,
|
||
|
|
input=input_bytes,
|
||
|
|
check=True,
|
||
|
|
capture_output=True,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def encrypt_ext4_image_luks2(
|
||
|
|
ext4_img_path,
|
||
|
|
passphrase,
|
||
|
|
pbkdf_memory_kb=DEFAULT_PBKDF_MEMORY_KB,
|
||
|
|
pbkdf_iterations=DEFAULT_PBKDF_ITERATIONS,
|
||
|
|
pbkdf_parallel=DEFAULT_PBKDF_PARALLEL,
|
||
|
|
):
|
||
|
|
"""Encrypt an ext4 image in place inside a LUKS2 (aes-xts-plain64, Argon2id) container."""
|
||
|
|
validate_luks_passphrase(passphrase)
|
||
|
|
abs_img = os.path.abspath(ext4_img_path)
|
||
|
|
if not os.path.isfile(abs_img):
|
||
|
|
raise FileNotFoundError(f"ext4 image not found: {abs_img}")
|
||
|
|
|
||
|
|
pad_bytes = LUKS2_HEADER_PAD_MIB * 1024 * 1024
|
||
|
|
with open(abs_img, "r+b") as f:
|
||
|
|
f.seek(0, os.SEEK_END)
|
||
|
|
cur_sz = f.tell()
|
||
|
|
f.truncate(cur_sz + pad_bytes)
|
||
|
|
|
||
|
|
use_host = shutil.which("cryptsetup") is not None
|
||
|
|
img_dir = os.path.dirname(abs_img)
|
||
|
|
img_arg = abs_img if use_host else f"/img/{os.path.basename(abs_img)}"
|
||
|
|
pw_bytes = passphrase.encode("utf-8")
|
||
|
|
|
||
|
|
_run_cryptsetup(
|
||
|
|
[
|
||
|
|
"reencrypt", "--encrypt",
|
||
|
|
"--disable-locks",
|
||
|
|
"--type", "luks2",
|
||
|
|
"--cipher", "aes-xts-plain64",
|
||
|
|
"--key-size", "512",
|
||
|
|
"--hash", "sha256",
|
||
|
|
"--pbkdf", "argon2id",
|
||
|
|
"--pbkdf-memory", str(int(pbkdf_memory_kb)),
|
||
|
|
"--pbkdf-force-iterations", str(int(pbkdf_iterations)),
|
||
|
|
"--pbkdf-parallel", str(int(pbkdf_parallel)),
|
||
|
|
"--reduce-device-size", f"{LUKS2_HEADER_PAD_MIB}M",
|
||
|
|
"--batch-mode",
|
||
|
|
"--key-file", "-",
|
||
|
|
img_arg,
|
||
|
|
],
|
||
|
|
img_dir,
|
||
|
|
input_bytes=pw_bytes,
|
||
|
|
)
|
||
|
|
|
||
|
|
_run_cryptsetup(["isLuks", "--disable-locks", img_arg], img_dir)
|
||
|
|
_run_cryptsetup(
|
||
|
|
["open", "--test-passphrase", "--disable-locks", "--key-file", "-", img_arg],
|
||
|
|
img_dir,
|
||
|
|
input_bytes=pw_bytes,
|
||
|
|
)
|