#!/usr/bin/env bash # shellcheck shell=bash # # LUKS2 encryption backend. # # Passphrases are held in memory only (LUKS_PASSPHRASE), never written to the # config, never passed on a command line and never logged. cryptsetup reads the # passphrase from stdin via --key-file -. # # TPM2 enrollment is intentionally not implemented in v0.1; the function # stubs document where it will hook in. declare -g LUKS_PASSPHRASE="${LUKS_PASSPHRASE:-}" encryption_require_tools() { require_cmd cryptsetup blkid || return 1 } # encryption_configure - prompt for and remember the passphrase (TUI or tty) encryption_configure() { local first second if declare -F ui_passwordbox >/dev/null 2>&1; then ui_passwordbox "Encryption passphrase" "Enter a LUKS2 passphrase for the root volume:" || return 1 first="$UI_RESULT" ui_passwordbox "Encryption passphrase" "Repeat the passphrase:" || return 1 second="$UI_RESULT" else read -r -s -p "LUKS passphrase: " first; printf '\n' >&2 read -r -s -p "Repeat: " second; printf '\n' >&2 fi if [[ -z "$first" ]]; then log_error "empty encryption passphrase refused" return 1 fi if [[ "$first" != "$second" ]]; then log_error "passphrases did not match" return 1 fi LUKS_PASSPHRASE="$first" secret_register "$first" log_info "encryption passphrase set (value not logged)" return 0 } # _run_cryptsetup_stdin DESCRIPTION COMMAND... - feeds LUKS_PASSPHRASE on stdin _run_cryptsetup_stdin() { local desc="$1"; shift if config_bool DRY_RUN; then log_line INFO "[dry-run] $desc :: $(_cmd_to_string "$@")" return 0 fi log_line INFO "$desc :: $(_cmd_to_string "$@")" printf '%s' "$LUKS_PASSPHRASE" | "$@" >>"$PHOEBUS_LOG_FILE" 2>&1 local status=$? log_line INFO "$desc :: exit status $status" return $status } # encryption_format PARTITION encryption_format() { local part="$1" cipher keysize pbkdf cipher="$(config_get LUKS_CIPHER)" keysize="$(config_get LUKS_KEY_SIZE)" pbkdf="$(config_get LUKS_PBKDF)" [[ -n "$LUKS_PASSPHRASE" ]] || { log_error "no passphrase configured"; return 1; } _run_cryptsetup_stdin "format LUKS2 on $part" \ cryptsetup luksFormat --batch-mode --type luks2 \ --cipher "$cipher" --key-size "$keysize" --pbkdf "$pbkdf" \ --key-file - "$part" } # encryption_open PARTITION NAME encryption_open() { local part="$1" name="${2:-$(config_get LUKS_NAME)}" [[ -n "$LUKS_PASSPHRASE" ]] || { log_error "no passphrase configured"; return 1; } _run_cryptsetup_stdin "open LUKS2 $part as $name" \ cryptsetup open --type luks --key-file - "$part" "$name" } # encryption_close NAME encryption_close() { local name="${1:-$(config_get LUKS_NAME)}" run_destructive "close LUKS device $name" cryptsetup close "$name" } # encryption_mapper_path NAME encryption_mapper_path() { printf '/dev/mapper/%s' "${1:-$(config_get LUKS_NAME)}" } # encryption_crypttab_line PARTITION [NAME] # Produces a crypttab line using the partition UUID (stable across reboots). encryption_crypttab_line() { local part="$1" name="${2:-$(config_get LUKS_NAME)}" uuid uuid="$(blkid -s UUID -o value "$part" 2>/dev/null)" [[ -n "$uuid" ]] || return 1 printf '%s UUID=%s none luks,discard\n' "$name" "$uuid" } # encryption_tpm2_enroll - future work; never made mandatory encryption_tpm2_enroll() { log_warn "TPM2 enrollment is not implemented in this version" return 1 } # encryption_tpm2_available - best-effort capability probe encryption_tpm2_available() { [[ -e /sys/class/tpm/tpm0 ]] && command -v systemd-cryptenroll >/dev/null 2>&1 }