sha256 in "AF hash" despite using sha512 during luksFormat

"[email protected]" <[email protected]>
Newsgroups dev.linux.lists.cryptsetup
Message-ID <CACHosL-Nxs5dHJddw8RjqnK4d7OewzA2FT6G1ZQcAhw=PzLLpQ@mail.gmail.com>
Dear cryptsetup/LUKS Team,

I was using sha512 in the luksFormat command.
Later I used luksAddKey while thinking that it should be using the
sha512 hash that I defined in luksFormat.
But, when I did luksDump, then I noticed that the field "AF hash" for
the second key (which was added via luksAddKey ; its keyslot is #1)
contains the value "sha256".
I expected it to contain sha512.
Notice that keyslot#0 has "sha512" in its corresponding "AF hash" field.

Attached script which reproduces that issue, filename
"reproduce_commands_without_hash.sh".
Attached output of the script, filename
"reproduce_commands_without_hash.log.txt".

Did I miss something?

Best regards,
David.
reproduce_commands_without_hash.log.txt (text/plain, 10.9 KB)
LUKS Format:
# cryptsetup 2.4.3 processing "cryptsetup --type=luks2 --verbose --debug --hash sha512 --key-size 512 --header /tmp/header.img --key-file - --iter-time=50 luksFormat /dev/sdb1"
# Running command luksFormat.
# Locking memory.
# Installing SIGINT/SIGTERM handler.
# Unblocking interruption on signal.
# Allocating context for crypt device /tmp/header.img.
# Trying to open and read device /tmp/header.img with direct-io.
# Trying to open device /tmp/header.img without direct-io.
# Initialising device-mapper backend library.
WARNING: Device /tmp/header.img already contains a 'crypto_LUKS' superblock signature.
# STDIN descriptor passphrase entry requested.
# Crypto backend (OpenSSL 3.0.2 15 Mar 2022 [default][legacy]) initialized in cryptsetup library version 2.4.3.
# Detected kernel Linux 5.15.0-43-generic x86_64.
# PBKDF argon2id, time_ms 50 (iterations 0), max_memory_kb 1048576, parallel_threads 4.
Existing 'crypto_LUKS' superblock signature on device /tmp/header.img will be wiped.
Existing 'crypto_LUKS' superblock signature on device /tmp/header.img will be wiped.
# Formatting device /tmp/header.img as type LUKS2.
# Auto-detected optimal encryption sector size for device /tmp/header.img is 4096 bytes.
# dm version   [ opencount flush ]   [16384] (*1)
# dm versions   [ opencount flush ]   [16384] (*1)
# Detected dm-ioctl version 4.45.0.
# Device-mapper backend running with UDEV support enabled.
# Trying to open and read device /dev/sdb1 with direct-io.
# Checking if cipher aes-xts-plain64 is usable.
# Using userspace crypto wrapper to access keyslot area.
# Formatting LUKS2 with JSON metadata area 12288 bytes and keyslots area 16744448 bytes.
# Creating new digest 0 (pbkdf2).
# Setting PBKDF2 type key digest 0.
# Running pbkdf2(sha512) benchmark.
# PBKDF benchmark: memory cost = 0, iterations = 1638400, threads = 0 (took 20 ms)
# PBKDF benchmark: memory cost = 0, iterations = 1741820, threads = 0 (took 301 ms)
# PBKDF benchmark: memory cost = 0, iterations = 1744718, threads = 0 (took 601 ms)
# Benchmark returns pbkdf2(sha512) 1744718 iterations, 0 memory, 0 threads (for 512-bits key).
# Segment 0 assigned to digest 0.
# Wiping LUKS areas (0x000000 - 0x001000) with zeroes.
# Wiping keyslots area (0x008000 - 0x1000000) with random data.
# Reusing open rw fd on device /tmp/header.img
# Device size 16777216, offset 16777216.
# Acquiring write lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img WRITE lock taken.
# Trying to write LUKS2 header (16384 bytes) at offset 0.
# Reusing open rw fd on device /tmp/header.img
# Checksum:7080b40cfa78985838b93520b7c434e4abe4f4352f431614b57ac4a13eb87363 (in-memory)
# Trying to write LUKS2 header (16384 bytes) at offset 16384.
# Reusing open rw fd on device /tmp/header.img
# Checksum:3008ee2430be15229759d89ce03daf169a1a0cd40472bfbe0c6395fba356e427 (in-memory)
# Device /tmp/header.img WRITE lock released.
# Adding new keyslot -1 using volume key.
# Adding new keyslot -1 with volume key assigned to a crypt segment.
# Selected keyslot 0.
# Keyslot 0 assigned to digest 0.
# Trying to allocate LUKS2 keyslot 0.
# Found area 32768 -> 290816
# Running argon2id() benchmark.
# PBKDF benchmark: memory cost = 65536, iterations = 4, threads = 4 (took 60 ms)
# PBKDF benchmark: memory cost = 273066, iterations = 4, threads = 4 (took 228 ms)
# PBKDF benchmark: memory cost = 299414, iterations = 4, threads = 4 (took 252 ms)
# Benchmark returns argon2id() 4 iterations, 65536 memory, 4 threads (for 512-bits key).
# Calculating attributes for LUKS2 keyslot 0.
# Acquiring write lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img WRITE lock taken.
# Checking context sequence id matches value stored on disk.
# Reusing open ro fd on device /tmp/header.img
# Running keyslot key derivation.
# Updating keyslot area [0x8000].
# Reusing open rw fd on device /tmp/header.img
# Device size 16777216, offset 16777216.
# Device /tmp/header.img WRITE lock already held.
# Trying to write LUKS2 header (16384 bytes) at offset 0.
# Reusing open rw fd on device /tmp/header.img
# Checksum:7c8b2c722311412a14c3721ec7a01c4b11894701f4aa94500caf854cad8c6cc6 (in-memory)
# Trying to write LUKS2 header (16384 bytes) at offset 16384.
# Reusing open rw fd on device /tmp/header.img
# Checksum:f3826f590069d99638ea2cc1d58c976f29f39b1582fdac1a82c26b3ff3059696 (in-memory)
# Device /tmp/header.img WRITE lock released.
Key slot 0 created.
# Releasing crypt device /tmp/header.img context.
# Releasing device-mapper backend.
# Closing read only fd for /tmp/header.img.
# Closing read write fd for /tmp/header.img.
# Unlocking memory.
Command successful.

Add a key file:
# cryptsetup 2.4.3 processing "cryptsetup --verbose --debug --key-file - --iter-time=50 luksAddKey /tmp/header.img /tmp/key"
# Running command luksAddKey.
# Locking memory.
# Installing SIGINT/SIGTERM handler.
# Unblocking interruption on signal.
# Allocating context for crypt device /tmp/header.img.
# Trying to open and read device /tmp/header.img with direct-io.
# Trying to open device /tmp/header.img without direct-io.
# Initialising device-mapper backend library.
# Trying to load any crypt type from device /tmp/header.img.
# Crypto backend (OpenSSL 3.0.2 15 Mar 2022 [default][legacy]) initialized in cryptsetup library version 2.4.3.
# Detected kernel Linux 5.15.0-43-generic x86_64.
# Loading LUKS2 header (repair disabled).
# Acquiring read lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img READ lock taken.
# Trying to read primary LUKS2 header at offset 0x0.
# Opening locked device /tmp/header.img
# Verifying locked device handle (regular file)
# LUKS2 header version 2 of size 16384 bytes, checksum sha256.
# Checksum:7c8b2c722311412a14c3721ec7a01c4b11894701f4aa94500caf854cad8c6cc6 (on-disk)
# Checksum:7c8b2c722311412a14c3721ec7a01c4b11894701f4aa94500caf854cad8c6cc6 (in-memory)
# Trying to read secondary LUKS2 header at offset 0x4000.
# Reusing open ro fd on device /tmp/header.img
# LUKS2 header version 2 of size 16384 bytes, checksum sha256.
# Checksum:f3826f590069d99638ea2cc1d58c976f29f39b1582fdac1a82c26b3ff3059696 (on-disk)
# Checksum:f3826f590069d99638ea2cc1d58c976f29f39b1582fdac1a82c26b3ff3059696 (in-memory)
# Device size 16777216, offset 16777216.
# Device /tmp/header.img READ lock released.
# PBKDF argon2id, time_ms 2000 (iterations 0), max_memory_kb 1048576, parallel_threads 4.
# PBKDF argon2id, time_ms 50 (iterations 0), max_memory_kb 1048576, parallel_threads 4.
# STDIN descriptor passphrase entry requested.
# Checking volume passphrase [keyslot -1] using passphrase.
# Keyslot 0 priority 1 != 2 (required), skipped.
# Trying to open LUKS2 keyslot 0.
# Running keyslot key derivation.
# Reading keyslot area [0x8000].
# Acquiring read lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img READ lock taken.
# Reusing open ro fd on device /tmp/header.img
# Device /tmp/header.img READ lock released.
# Verifying key from keyslot 0, digest 0.
# dm version   [ opencount flush ]   [16384] (*1)
# dm versions   [ opencount flush ]   [16384] (*1)
# Detected dm-ioctl version 4.45.0.
# Device-mapper backend running with UDEV support enabled.
Key slot 0 unlocked.
# File descriptor passphrase entry requested.
# Adding new keyslot, existing passphrase provided,new passphrase provided.
# Selected keyslot 1.
# Keyslot 0 priority 1 != 2 (required), skipped.
# Trying to open LUKS2 keyslot 0.
# Running keyslot key derivation.
# Reading keyslot area [0x8000].
# Acquiring read lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img READ lock taken.
# Reusing open ro fd on device /tmp/header.img
# Device /tmp/header.img READ lock released.
# Verifying key from keyslot 0, digest 0.
# Keyslot 1 assigned to digest 0.
# Trying to allocate LUKS2 keyslot 1.
# Found area 290816 -> 548864
# Running argon2id() benchmark.
# PBKDF benchmark: memory cost = 65536, iterations = 4, threads = 4 (took 52 ms)
# PBKDF benchmark: memory cost = 315076, iterations = 4, threads = 4 (took 261 ms)
# Benchmark returns argon2id() 4 iterations, 65536 memory, 4 threads (for 512-bits key).
# Calculating attributes for LUKS2 keyslot 1.
# Acquiring write lock for device /tmp/header.img.
# Verifying lock handle for /tmp/header.img.
# Device /tmp/header.img WRITE lock taken.
# Checking context sequence id matches value stored on disk.
# Reusing open ro fd on device /tmp/header.img
# Running keyslot key derivation.
# Updating keyslot area [0x47000].
# Opening locked device /tmp/header.img
# Verifying locked device handle (regular file)
# Device size 16777216, offset 16777216.
# Device /tmp/header.img WRITE lock already held.
# Trying to write LUKS2 header (16384 bytes) at offset 0.
# Reusing open rw fd on device /tmp/header.img
# Checksum:3600aeb87da586f74cf113089e0e37faefe420338e89f24c65a18ba8ea329d37 (in-memory)
# Trying to write LUKS2 header (16384 bytes) at offset 16384.
# Reusing open rw fd on device /tmp/header.img
# Checksum:91a974803aa8ab158440983c51b9fdebb88b21240b168d1c4bb246a2443e00e1 (in-memory)
# Device /tmp/header.img WRITE lock released.
Key slot 1 created.
# Releasing crypt device /tmp/header.img context.
# Releasing device-mapper backend.
# Closing read only fd for /tmp/header.img.
# Closing read write fd for /tmp/header.img.
# Unlocking memory.
Command successful.

LUKS header information
Version:       	2
Epoch:         	4
Metadata area: 	16384 [bytes]
Keyslots area: 	16744448 [bytes]
UUID:          	49f5c9d2-00d3-4d5b-8a9a-0197077be6c9
Label:         	(no label)
Subsystem:     	(no subsystem)
Flags:       	(no flags)

Data segments:
  0: crypt
	offset: 0 [bytes]
	length: (whole device)
	cipher: aes-xts-plain64
	sector: 4096 [bytes]

Keyslots:
  0: luks2
	Key:        512 bits
	Priority:   normal
	Cipher:     aes-xts-plain64
	Cipher key: 512 bits
	PBKDF:      argon2id
	Time cost:  4
	Memory:     65536
	Threads:    4
	Salt:       a0 2f 35 ff 70 ab 22 d8 5f c8 93 29 63 8b 54 e9 
	            8a 18 f2 fe c6 dd 27 bf ba 61 07 2d 4a c0 9b f5 
	AF stripes: 4000
	AF hash:    sha512
	Area offset:32768 [bytes]
	Area length:258048 [bytes]
	Digest ID:  0
  1: luks2
	Key:        512 bits
	Priority:   normal
	Cipher:     aes-xts-plain64
	Cipher key: 512 bits
	PBKDF:      argon2id
	Time cost:  4
	Memory:     65536
	Threads:    4
	Salt:       a2 a0 a7 94 cb cc 61 84 cc 79 75 52 39 5a c1 4c 
	            1d 39 ee 2e f2 89 0c 53 81 e0 54 5e c9 1f 5a 5a 
	AF stripes: 4000
	AF hash:    sha256
	Area offset:290816 [bytes]
	Area length:258048 [bytes]
	Digest ID:  0
Tokens:
Digests:
  0: pbkdf2
	Hash:       sha512
	Iterations: 218089
	Salt:       1b 9a 78 f6 a5 fe 3d f7 70 8c be c8 a2 3a 4d 12 
	            1a aa 61 ff ad 56 d7 93 0b 93 15 10 b8 7a 6a af 
	Digest:     b8 15 55 0f 66 b2 4d bc 67 e3 a0 75 4a 4c b6 6f 
	            62 c3 22 c2 3c 11 9d a4 e7 4b 3f 63 bc d8 80 8e 
	            4f 16 58 61 cf 0e 8c a3 51 27 1c b1 ea 5a 02 fe 
	            1f 33 35 70 49 57 22 4e e5 63 9c 88 69 b4 1d 38
reproduce_commands_without_hash.sh (application/x-shellscript, 1.9 KB)
#!/usr/bin/env bash

# ---------

KEY_SIZE_BYTES=64
KEY_SIZE_BITS=$(( 8 * KEY_SIZE_BYTES ))

ITER_TIME=50 # Just to make it fast

WORKSPACE_DIR="/tmp"
LUKS_HEADER_FILEPATH="${WORKSPACE_DIR}/header.img"
KEY_FILEPATH="${WORKSPACE_DIR}/key"

DEVP="/dev/sdb"

# ---------

#
# Format and add a passphrase from a key file via stdin
#

echo "LUKS Format:"

PASSPHRASE="123"

cmd_cryptsetup_format_params=()

cmd_cryptsetup_format_params+=(--verbose)
cmd_cryptsetup_format_params+=(--debug)
cmd_cryptsetup_format_params+=(--hash sha512)
cmd_cryptsetup_format_params+=(--key-size ${KEY_SIZE_BITS})
cmd_cryptsetup_format_params+=(--header "${LUKS_HEADER_FILEPATH}")
cmd_cryptsetup_format_params+=(--key-file -)
cmd_cryptsetup_format_params+=(--iter-time=${ITER_TIME})

printf "%s" "${PASSPHRASE}" | cryptsetup --type=luks2 "${cmd_cryptsetup_format_params[@]}" luksFormat "${DEVP}1"
cryptsetup_exit_status=$?
echo ""
if [ $cryptsetup_exit_status -ne 0 ]; then
    echo "cryptsetup failed: $cryptsetup_exit_status."
    exit 1
fi

# ---------

#
# Add a key file
#

echo "Add a key file:"

# Create key file
head -c 32 /dev/urandom > "${KEY_FILEPATH}"
head_exit_status=$?
if [ $head_exit_status -ne 0 ]; then
    echo "cryptsetup failed: $cryptsetup_exit_status."
    exit 1
fi

cmd_cryptsetup_addkey_params=()

cmd_cryptsetup_addkey_params+=(--verbose)
cmd_cryptsetup_addkey_params+=(--debug)
cmd_cryptsetup_addkey_params+=(--key-file -)
cmd_cryptsetup_addkey_params+=(--iter-time=${ITER_TIME})

luksAddKey_device_param="${LUKS_HEADER_FILEPATH}"
printf "%s" "${PASSPHRASE}" | cryptsetup "${cmd_cryptsetup_addkey_params[@]}" luksAddKey "${luksAddKey_device_param}" "${KEY_FILEPATH}"
cryptsetup_exit_status=$?
echo ""
if [ $cryptsetup_exit_status -ne 0 ]; then
    echo "cryptsetup failed: $cryptsetup_exit_status."
    exit 1
fi

# ---------

#
# Dump LUKS header
#

cmd_cryptsetup_luksDump_device="${LUKS_HEADER_FILEPATH}"
cryptsetup luksDump "${cmd_cryptsetup_luksDump_device}"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.