2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc. All Rights Reserved.
5 # Functions for setting up and testing file encryption
8 # _require_scratch_encryption [-c CONTENTS_MODE] [-n FILENAMES_MODE]
9 # [-f POLICY_FLAGS] [-v POLICY_VERSION]
11 # Require encryption support on the scratch device.
13 # This checks for support for the default type of encryption policy (v1 with
14 # AES-256-XTS and AES-256-CTS). Options can be specified to also require
15 # support for a different type of encryption policy.
17 _require_scratch_encryption()
21 _require_xfs_io_command "set_encpolicy"
23 # The 'test_dummy_encryption' mount option interferes with trying to use
24 # encryption for real, even if we are just trying to get/set policies
25 # and never put any keys in the keyring. So skip the real encryption
26 # tests if the 'test_dummy_encryption' mount option was specified.
27 _exclude_scratch_mount_option "test_dummy_encryption"
29 # Make a filesystem on the scratch device with the encryption feature
30 # enabled. If this fails then probably the userspace tools (e.g.
31 # e2fsprogs or f2fs-tools) are too old to understand encryption.
32 if ! _scratch_mkfs_encrypted &>>$seqres.full; then
33 _notrun "$FSTYP userspace tools do not support encryption"
36 # Try to mount the filesystem. If this fails then either the kernel
37 # isn't aware of encryption, or the mkfs options were not compatible
38 # with encryption (e.g. ext4 with block size != PAGE_SIZE).
39 if ! _try_scratch_mount &>>$seqres.full; then
40 _notrun "kernel is unaware of $FSTYP encryption feature," \
41 "or mkfs options are not compatible with encryption"
44 # The kernel may be aware of encryption without supporting it. For
45 # example, for ext4 this is the case with kernels configured with
46 # CONFIG_EXT4_FS_ENCRYPTION=n. Detect support for encryption by trying
47 # to set an encryption policy. (For ext4 we could instead check for the
48 # presence of /sys/fs/ext4/features/encryption, but this is broken on
49 # some older kernels and is ext4-specific anyway.)
50 mkdir $SCRATCH_MNT/tmpdir
51 if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
52 egrep -q 'Inappropriate ioctl for device|Operation not supported'
54 _notrun "kernel does not support $FSTYP encryption"
56 rmdir $SCRATCH_MNT/tmpdir
58 # If required, check for support for the specific type of encryption
59 # policy required by the test.
61 _require_encryption_policy_support $SCRATCH_MNT "$@"
67 _require_encryption_policy_support()
71 local set_encpolicy_args=""
73 local policy_version=1
77 while getopts "c:n:f:v:" c; do
80 set_encpolicy_args+=" -$c $OPTARG"
83 set_encpolicy_args+=" -$c $OPTARG"
87 set_encpolicy_args+=" -$c $OPTARG"
88 policy_version=$OPTARG
91 _fail "Unrecognized option '$c'"
95 set_encpolicy_args=${set_encpolicy_args# }
97 echo "Checking whether kernel supports encryption policy: $set_encpolicy_args" \
100 if (( policy_flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 )); then
102 _scratch_mkfs_stable_inodes_encrypted &>> $seqres.full
107 if (( policy_version > 1 )); then
108 _require_xfs_io_command "get_encpolicy" "-t"
109 local output=$(_get_encpolicy $dir -t)
110 if [ "$output" != "supported" ]; then
111 if [ "$output" = "unsupported" ]; then
112 _notrun "kernel does not support $FSTYP encryption v2 API"
114 _fail "Unexpected output from 'get_encpolicy -t': $output"
116 # Both the kernel and xfs_io support v2 encryption policies, and
117 # therefore also filesystem-level keys -- since that's the only
118 # way to provide keys for v2 policies.
119 local raw_key=$(_generate_raw_encryption_key)
120 local keyspec=$(_add_enckey $mnt "$raw_key" | awk '{print $NF}')
122 _require_command "$KEYCTL_PROG" keyctl
124 local keyspec=$(_generate_session_encryption_key)
126 if _set_encpolicy $dir $keyspec $set_encpolicy_args \
127 2>&1 >>$seqres.full | egrep -q 'Invalid argument'; then
128 _notrun "kernel does not support encryption policy: '$set_encpolicy_args'"
130 # fscrypt allows setting policies with modes it knows about, even
131 # without kernel crypto API support. E.g. a policy using Adiantum
132 # encryption can be set on a kernel without CONFIG_CRYPTO_ADIANTUM.
133 # But actually trying to use such an encrypted directory will fail.
134 # To reliably check for availability of both the contents and filenames
135 # encryption modes, try creating a nonempty file.
136 if ! echo foo > $dir/file; then
137 _notrun "encryption policy '$set_encpolicy_args' is unusable; probably missing kernel crypto API support"
139 if (( policy_version <= 1 )); then
140 $KEYCTL_PROG clear @s
145 _scratch_mkfs_encrypted()
149 _scratch_mkfs -O encrypt
152 # erase the UBI volume; reformated automatically on next mount
153 $UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
156 _notrun "No encryption support for $FSTYP"
161 _scratch_mkfs_sized_encrypted()
165 MKFS_OPTIONS="$MKFS_OPTIONS -O encrypt" _scratch_mkfs_sized $*
168 _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized_encrypted"
173 # Like _scratch_mkfs_encrypted(), but add -O stable_inodes if applicable for the
174 # filesystem type. This is necessary for using encryption policies that include
175 # the inode number in the IVs, e.g. policies with the IV_INO_LBLK_64 flag set.
176 _scratch_mkfs_stable_inodes_encrypted()
180 if ! _scratch_mkfs -O encrypt -O stable_inodes; then
181 _notrun "-O stable_inodes is not supported"
185 _scratch_mkfs_encrypted
190 # For some tests it's helpful to always use the same key so that the test's
191 # output is always the same. For this purpose the following key can be used:
194 TEST_RAW_KEY+="\\x$(printf "%02x" $i)"
196 # Key descriptor: arbitrary value
197 TEST_KEY_DESCRIPTOR="0000111122223333"
198 # Key identifier: HKDF-SHA512(key=$TEST_RAW_KEY, salt="", info="fscrypt\0\x01")
199 TEST_KEY_IDENTIFIER="69b2f6edeee720cce0577937eb8a6751"
201 # Give the invoking shell a new session keyring. This makes any keys we add to
202 # the session keyring scoped to the lifetime of the test script.
203 _new_session_keyring()
205 $KEYCTL_PROG new_session >>$seqres.full
208 # Generate a key descriptor (16 character hex string)
209 _generate_key_descriptor()
213 for ((i = 0; i < 8; i++)); do
214 keydesc="${keydesc}$(printf "%02x" $(( $RANDOM % 256 )))"
219 # Generate a raw encryption key, but don't add it to any keyring yet.
220 _generate_raw_encryption_key()
224 for ((i = 0; i < 64; i++)); do
225 raw="${raw}\\x$(printf "%02x" $(( $RANDOM % 256 )))"
230 # Serialize an integer into a CPU-endian bytestring of the given length, and
231 # print it as a string where each byte is hex-escaped. For example,
232 # `_num_to_hex 1000 4` == "\xe8\x03\x00\x00" if the CPU is little endian.
238 local big_endian=$(echo -ne '\x11' | od -tx2 | head -1 | \
239 cut -f2 -d' ' | cut -c1)
241 if (( big_endian )); then
242 for (( i = 0; i < nbytes; i++ )); do
243 printf '\\x%02x' $(((value >> (8*(nbytes-1-i))) & 0xff))
246 for (( i = 0; i < nbytes; i++ )); do
247 printf '\\x%02x' $(((value >> (8*i)) & 0xff))
252 # Add the specified raw encryption key to the session keyring, using the
253 # specified key descriptor.
254 _add_session_encryption_key()
260 # Add the key to the session keyring. The required structure is:
262 # #define FSCRYPT_MAX_KEY_SIZE 64
263 # struct fscrypt_key {
265 # __u8 raw[FSCRYPT_MAX_KEY_SIZE];
269 # The kernel ignores 'mode' but requires that 'size' be 64.
271 # Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
272 # descriptor hex string. Newer kernels (ext4 4.8 and later, f2fs 4.6
273 # and later) also allow the common key prefix "fscrypt:" in addition to
274 # their filesystem-specific key prefix ("ext4:", "f2fs:"). It would be
275 # nice to use the common key prefix, but for now use the filesystem-
276 # specific prefix to make it possible to test older kernels...
278 local mode=$(_num_to_hex 0 4)
279 local size=$(_num_to_hex 64 4)
280 echo -n -e "${mode}${raw}${size}" |
281 $KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
285 # Generate a random encryption key, add it to the session keyring, and print out
286 # the resulting key descriptor (example: "8bf798e1a494e1ec"). Requires the
287 # keyctl program. It's assumed the caller has already set up a test-scoped
288 # session keyring using _new_session_keyring.
290 _generate_session_encryption_key()
292 local keydesc=$(_generate_key_descriptor)
293 local raw=$(_generate_raw_encryption_key)
295 _add_session_encryption_key $keydesc $raw
300 # Unlink an encryption key from the session keyring, given its key descriptor.
301 _unlink_session_encryption_key()
304 local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
305 $KEYCTL_PROG unlink $keyid >>$seqres.full
308 # Revoke an encryption key from the session keyring, given its key descriptor.
309 _revoke_session_encryption_key()
312 local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
313 $KEYCTL_PROG revoke $keyid >>$seqres.full
316 # Set an encryption policy on the specified directory.
322 $XFS_IO_PROG -c "set_encpolicy $*" "$dir"
325 _user_do_set_encpolicy()
330 _user_do "$XFS_IO_PROG -c \"set_encpolicy $*\" \"$dir\""
333 # Display the specified file or directory's encryption policy.
339 $XFS_IO_PROG -c "get_encpolicy $*" "$file"
342 _user_do_get_encpolicy()
347 _user_do "$XFS_IO_PROG -c \"get_encpolicy $*\" \"$file\""
350 # Add an encryption key to the given filesystem.
357 echo -ne "$raw_key" | $XFS_IO_PROG -c "add_enckey $*" "$mnt"
360 _user_do_add_enckey()
366 _user_do "echo -ne \"$raw_key\" | $XFS_IO_PROG -c \"add_enckey $*\" \"$mnt\""
369 # Remove the given encryption key from the given filesystem.
376 $XFS_IO_PROG -c "rm_enckey $* $keyspec" "$mnt"
385 _user_do "$XFS_IO_PROG -c \"rm_enckey $* $keyspec\" \"$mnt\""
388 # Get the status of the given encryption key on the given filesystem.
395 $XFS_IO_PROG -c "enckey_status $* $keyspec" "$mnt"
398 _user_do_enckey_status()
404 _user_do "$XFS_IO_PROG -c \"enckey_status $* $keyspec\" \"$mnt\""
407 # Require support for adding a key to a filesystem's fscrypt keyring via an
408 # "fscrypt-provisioning" keyring key.
409 _require_add_enckey_by_key_id()
414 _require_xfs_io_command "add_enckey" "-k"
417 if $XFS_IO_PROG -c "add_enckey -k 12345" "$mnt" \
418 |& grep -q 'Invalid argument'; then
419 _notrun "Kernel doesn't support key_id parameter to FS_IOC_ADD_ENCRYPTION_KEY"
423 # Add a key of type "fscrypt-provisioning" to the session keyring and print the
425 _add_fscrypt_provisioning_key()
431 # The format of the key payload must be:
433 # struct fscrypt_provisioning_key_payload {
439 local type_hex=$(_num_to_hex $type 4)
440 local reserved=$(_num_to_hex 0 4)
441 echo -n -e "${type_hex}${reserved}${raw}" |
442 $KEYCTL_PROG padd fscrypt-provisioning "$desc" @s
445 # Retrieve the encryption nonce of the given inode as a hex string. The nonce
446 # was randomly generated by the filesystem and isn't exposed directly to
447 # userspace. But it can be read using the filesystem's debugging tools.
448 _get_encryption_nonce()
455 # Use debugfs to dump the special xattr named "c", which is the
456 # file's fscrypt_context. This produces a line like:
458 # c (28) = 01 01 04 02 00 00 00 00 00 00 00 00 ef bd 18 76 5d f6 41 4e c0 a2 cd 5f 91 29 7e 12
460 # Then filter it to get just the 16-byte 'nonce' field at the end:
462 # efbd18765df6414ec0a2cd5f91297e12
464 $DEBUGFS_PROG $device -R "ea_get <$inode> c" 2>>$seqres.full \
465 | grep '^c ' | sed 's/^.*=//' | tr -d ' \n' | tail -c 32
468 # dump.f2fs prints the fscrypt_context like:
470 # xattr: e_name_index:9 e_name:c e_name_len:1 e_value_size:28 e_value:
472 # contents_encryption_mode: 0x1
473 # filenames_encryption_mode: 0x4
475 # master_key_descriptor: 0000000000000000
476 # nonce: EFBD18765DF6414EC0A2CD5F91297E12
478 # Also support the case where the whole xattr is printed as hex,
479 # as is the case for fscrypt_context_v2.
481 # xattr: e_name_index:9 e_name:c e_name_len:1 e_value_size:40 e_value:
482 # 020104020000000033809BFEBE68A4AD264079B30861DD5E6B9E72D07523C58794ACF52534BAA756
484 $DUMP_F2FS_PROG -i $inode $device | awk '
485 /\<e_name:c\>/ { found = 1 }
486 (/^nonce:/ || /^[[:xdigit:]]+$/) && found {
487 print substr($0, length($0) - 31, 32);
492 _fail "_get_encryption_nonce() isn't implemented on $FSTYP"
497 # Require support for _get_encryption_nonce()
498 _require_get_encryption_nonce_support()
500 echo "Checking for _get_encryption_nonce() support for $FSTYP" >> $seqres.full
503 _require_command "$DEBUGFS_PROG" debugfs
506 _require_command "$DUMP_F2FS_PROG" dump.f2fs
507 # For fscrypt_context_v2, we actually need a f2fs-tools version
508 # that has the patch "f2fs-tools: improve xattr value printing"
509 # (https://sourceforge.net/p/linux-f2fs/mailman/message/36648640/).
510 # Otherwise the xattr is incorrectly parsed as v1. But just let
511 # the test fail in that case, as it was an f2fs-tools bug...
514 _notrun "_get_encryption_nonce() isn't implemented on $FSTYP"
519 # Retrieve the encrypted filename stored on-disk for the given file.
520 # The name is printed to stdout in binary.
521 _get_ciphertext_filename()
529 # Extract the filename from the debugfs output line like:
531 # 131075 100644 (1) 0 0 0 22-Apr-2019 16:54 \xa2\x85\xb0z\x13\xe9\x09\x86R\xed\xdc\xce\xad\x14d\x19
533 # Bytes are shown either literally or as \xHH-style escape
534 # sequences; we have to decode the escaped bytes here.
536 $DEBUGFS_PROG $device -R "ls -l -r <$dir_inode>" \
537 2>>$seqres.full | perl -ne '
538 next if not /^\s*'$inode'\s+/;
541 s/\\x([[:xdigit:]]{2})/chr hex $1/eg;
545 # Extract the filename from the dump.f2fs output line like:
547 # i_name [UpkzIPuts9by1oDmE+Ivfw]
549 # The name is shown base64-encoded; we have to decode it here.
551 $DUMP_F2FS_PROG $device -i $inode | perl -ne '
552 next if not /^i_name\s+\[([A-Za-z0-9+,]+)\]/;
554 my @chars = split //, $1;
557 my $table = join "", (A..Z, a..z, 0..9, "+", ",");
559 $ac += index($table, $_) << $bits;
562 print chr($ac & 0xff);
568 print STDERR "Invalid base64-encoded string!\n";
572 _fail "_get_ciphertext_filename() isn't implemented on $FSTYP"
577 # Require support for _get_ciphertext_filename().
578 _require_get_ciphertext_filename_support()
580 echo "Checking for _get_ciphertext_filename() support for $FSTYP" >> $seqres.full
583 # Verify that the "ls -l -r" debugfs command is supported and
584 # that it hex-encodes non-ASCII characters, rather than using an
585 # ambiguous escaping method. This requires e2fsprogs v1.45.1 or
586 # later; or more specifically, a version that has the commit
587 # "debugfs: avoid ambiguity when printing filenames".
588 _require_command "$DEBUGFS_PROG" debugfs
590 touch $SCRATCH_MNT/$'\xc1'
592 if ! $DEBUGFS_PROG $SCRATCH_DEV -R "ls -l -r /" 2>&1 \
593 | tee -a $seqres.full | grep -E -q '\s+\\xc1\s*$'; then
594 _notrun "debugfs (e2fsprogs) is too old; doesn't support showing unambiguous on-disk filenames"
598 # Verify that dump.f2fs shows encrypted filenames in full. This
599 # requires f2fs-tools v1.13.0 or later; or more specifically, a
600 # version that has the commit
601 # "f2fs-tools: improve filename printing".
603 _require_command "$DUMP_F2FS_PROG" dump.f2fs
604 _require_command "$KEYCTL_PROG" keyctl
608 local keydesc=$(_generate_session_encryption_key)
609 local dir=$SCRATCH_MNT/test.${FUNCNAME[0]}
610 local file=$dir/$(perl -e 'print "A" x 255')
612 _set_encpolicy $dir $keydesc
614 local inode=$(stat -c %i $file)
617 $KEYCTL_PROG clear @s
619 # 255-character filename should result in 340 base64 characters.
620 if ! $DUMP_F2FS_PROG -i $inode $SCRATCH_DEV \
621 | grep -E -q '^i_name[[:space:]]+\[[A-Za-z0-9+,]{340}\]'; then
622 _notrun "dump.f2fs (f2fs-tools) is too old; doesn't support showing unambiguous on-disk filenames"
626 _notrun "_get_ciphertext_filename() isn't implemented on $FSTYP"
631 # Get an encrypted file's list of on-disk blocks as a comma-separated list of
632 # block offsets from the start of the device. "Blocks" are 512 bytes each here.
633 _get_ciphertext_block_list()
638 $XFS_IO_PROG -c fiemap $file | perl -ne '
639 next if not /^\s*\d+: \[\d+\.\.\d+\]: (\d+)\.\.(\d+)/;
640 print $_ . "," foreach $1..$2;' | sed 's/,$//'
643 # Dump a block list that was previously saved by _get_ciphertext_block_list().
644 _dump_ciphertext_blocks()
650 for block in $(tr ',' ' ' <<< $blocklist); do
651 dd if=$device bs=512 count=1 skip=$block status=none
655 _do_verify_ciphertext_for_encryption_policy()
657 local contents_encryption_mode=$1
658 local filenames_encryption_mode=$2
659 local policy_flags=$3
660 local set_encpolicy_args=$4
663 local crypt_contents_cmd="$here/src/fscrypt-crypt-util $7"
664 local crypt_filename_cmd="$here/src/fscrypt-crypt-util $8"
666 local blocksize=$(_get_block_size $SCRATCH_MNT)
667 local test_contents_files=()
668 local test_filenames_files=()
669 local i src dir dst inode blocklist \
670 padding_flag padding dir_inode len name f nonce decrypted_name
672 # Create files whose encrypted contents we'll verify. For each, save
673 # the information: (copy of original file, inode number of encrypted
674 # file, comma-separated block list) into test_contents_files[].
675 echo "Creating files for contents verification" >> $seqres.full
677 rm -f $tmp.testfile_*
678 for src in /dev/zero /dev/urandom; do
679 head -c $((4 * blocksize)) $src > $tmp.testfile_$i
682 dir=$SCRATCH_MNT/encdir
684 _set_encpolicy $dir $keyspec $set_encpolicy_args -f $policy_flags
685 for src in $tmp.testfile_*; do
688 inode=$(stat -c %i $dst)
689 blocklist=$(_get_ciphertext_block_list $dst)
690 test_contents_files+=("$src $inode $blocklist")
693 # Create files whose encrypted names we'll verify. For each, save the
694 # information: (original filename, inode number of encrypted file, inode
695 # of parent directory, padding amount) into test_filenames_files[]. Try
696 # each padding amount: 4, 8, 16, or 32 bytes. Also try various filename
697 # lengths, including boundary cases. Assume NAME_MAX == 255.
698 echo "Creating files for filenames verification" >> $seqres.full
699 for padding_flag in 0 1 2 3; do
700 padding=$((4 << padding_flag))
701 dir=$SCRATCH_MNT/encdir.pad$padding
703 dir_inode=$(stat -c %i $dir)
704 _set_encpolicy $dir $keyspec $set_encpolicy_args \
705 -f $((policy_flags | padding_flag))
706 for len in 1 3 15 16 17 32 100 254 255; do
707 name=$(tr -d -C a-zA-Z0-9 < /dev/urandom | head -c $len)
709 inode=$(stat -c %i $dir/$name)
710 test_filenames_files+=("$name $inode $dir_inode $padding")
714 # Now unmount the filesystem and verify the ciphertext we just wrote.
717 echo "Verifying encrypted file contents" >> $seqres.full
718 for f in "${test_contents_files[@]}"; do
719 read -r src inode blocklist <<< "$f"
720 nonce=$(_get_encryption_nonce $SCRATCH_DEV $inode)
721 _dump_ciphertext_blocks $SCRATCH_DEV $blocklist > $tmp.actual_contents
722 $crypt_contents_cmd $contents_encryption_mode $raw_key_hex \
723 --file-nonce=$nonce --block-size=$blocksize \
724 --inode-number=$inode < $src > $tmp.expected_contents
725 if ! cmp $tmp.expected_contents $tmp.actual_contents; then
726 _fail "Expected encrypted contents != actual encrypted contents. File: $f"
728 $crypt_contents_cmd $contents_encryption_mode $raw_key_hex \
729 --decrypt --file-nonce=$nonce --block-size=$blocksize \
730 --inode-number=$inode \
731 < $tmp.actual_contents > $tmp.decrypted_contents
732 if ! cmp $src $tmp.decrypted_contents; then
733 _fail "Contents decryption sanity check failed. File: $f"
737 echo "Verifying encrypted file names" >> $seqres.full
738 for f in "${test_filenames_files[@]}"; do
739 read -r name inode dir_inode padding <<< "$f"
740 nonce=$(_get_encryption_nonce $SCRATCH_DEV $dir_inode)
741 _get_ciphertext_filename $SCRATCH_DEV $inode $dir_inode \
744 $crypt_filename_cmd $filenames_encryption_mode \
745 $raw_key_hex --file-nonce=$nonce --padding=$padding \
746 --block-size=255 --inode-number=$dir_inode \
748 if ! cmp $tmp.expected_name $tmp.actual_name; then
749 _fail "Expected encrypted filename != actual encrypted filename. File: $f"
751 $crypt_filename_cmd $filenames_encryption_mode $raw_key_hex \
752 --decrypt --file-nonce=$nonce --padding=$padding \
753 --block-size=255 --inode-number=$dir_inode \
754 < $tmp.actual_name > $tmp.decrypted_name
755 decrypted_name=$(tr -d '\0' < $tmp.decrypted_name)
756 if [ "$name" != "$decrypted_name" ]; then
757 _fail "Filename decryption sanity check failed ($name != $decrypted_name). File: $f"
762 # fscrypt UAPI constants (see <linux/fscrypt.h>)
764 FSCRYPT_MODE_AES_256_XTS=1
765 FSCRYPT_MODE_AES_256_CTS=4
766 FSCRYPT_MODE_AES_128_CBC=5
767 FSCRYPT_MODE_AES_128_CTS=6
768 FSCRYPT_MODE_ADIANTUM=9
770 FSCRYPT_POLICY_FLAG_DIRECT_KEY=0x04
771 FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64=0x08
773 FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR=1
774 FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER=2
776 _fscrypt_mode_name_to_num()
781 AES-256-XTS) echo $FSCRYPT_MODE_AES_256_XTS ;;
782 AES-256-CTS-CBC) echo $FSCRYPT_MODE_AES_256_CTS ;;
783 AES-128-CBC-ESSIV) echo $FSCRYPT_MODE_AES_128_CBC ;;
784 AES-128-CTS-CBC) echo $FSCRYPT_MODE_AES_128_CTS ;;
785 Adiantum) echo $FSCRYPT_MODE_ADIANTUM ;;
786 *) _fail "Unknown fscrypt mode: $name" ;;
790 # Verify that file contents and names are encrypted correctly when an encryption
791 # policy of the specified type is used.
793 # The first two parameters are the contents and filenames encryption modes to
794 # test. The following optional parameters are also accepted to further modify
795 # the type of encryption policy that is tested:
797 # 'v2': test a v2 encryption policy
798 # 'direct': test the DIRECT_KEY policy flag
799 # 'iv_ino_lblk_64': test the IV_INO_LBLK_64 policy flag
801 _verify_ciphertext_for_encryption_policy()
803 local contents_encryption_mode=$1
804 local filenames_encryption_mode=$2
806 local policy_version=1
808 local set_encpolicy_args=""
809 local crypt_util_args=""
810 local crypt_util_contents_args=""
811 local crypt_util_filename_args=""
820 if [ $contents_encryption_mode != \
821 $filenames_encryption_mode ]; then
822 _fail "For direct key mode, contents and filenames modes must match"
824 (( policy_flags |= FSCRYPT_POLICY_FLAG_DIRECT_KEY ))
827 (( policy_flags |= FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 ))
830 _fail "Unknown option '$opt' passed to ${FUNCNAME[0]}"
834 local contents_mode_num=$(_fscrypt_mode_name_to_num $contents_encryption_mode)
835 local filenames_mode_num=$(_fscrypt_mode_name_to_num $filenames_encryption_mode)
837 set_encpolicy_args+=" -c $contents_mode_num"
838 set_encpolicy_args+=" -n $filenames_mode_num"
840 if (( policy_version > 1 )); then
841 set_encpolicy_args+=" -v 2"
842 crypt_util_args+=" --kdf=HKDF-SHA512"
843 if (( policy_flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY )); then
844 if (( policy_flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 )); then
845 _fail "'direct' and 'iv_ino_lblk_64' options are mutually exclusive"
847 crypt_util_args+=" --mode-num=$contents_mode_num"
848 elif (( policy_flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 )); then
849 crypt_util_args+=" --iv-ino-lblk-64"
850 crypt_util_contents_args+=" --mode-num=$contents_mode_num"
851 crypt_util_filename_args+=" --mode-num=$filenames_mode_num"
854 if (( policy_flags & ~FSCRYPT_POLICY_FLAG_DIRECT_KEY )); then
855 _fail "unsupported flags for v1 policy: $policy_flags"
857 if (( policy_flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY )); then
858 crypt_util_args+=" --kdf=none"
860 crypt_util_args+=" --kdf=AES-128-ECB"
863 set_encpolicy_args=${set_encpolicy_args# }
865 _require_scratch_encryption $set_encpolicy_args -f $policy_flags
866 _require_test_program "fscrypt-crypt-util"
867 _require_xfs_io_command "fiemap"
868 _require_get_encryption_nonce_support
869 _require_get_ciphertext_filename_support
870 if (( policy_version == 1 )); then
871 _require_command "$KEYCTL_PROG" keyctl
874 echo "Creating encryption-capable filesystem" >> $seqres.full
875 if (( policy_flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 )); then
876 _scratch_mkfs_stable_inodes_encrypted &>> $seqres.full
878 _scratch_mkfs_encrypted &>> $seqres.full
882 crypt_util_args+=" --fs-uuid=$(blkid -s UUID -o value $SCRATCH_DEV | tr -d -)"
884 crypt_util_contents_args+="$crypt_util_args"
885 crypt_util_filename_args+="$crypt_util_args"
887 echo "Generating encryption key" >> $seqres.full
888 local raw_key=$(_generate_raw_encryption_key)
889 if (( policy_version > 1 )); then
890 local keyspec=$(_add_enckey $SCRATCH_MNT "$raw_key" \
893 local keyspec=$(_generate_key_descriptor)
895 _add_session_encryption_key $keyspec $raw_key
897 local raw_key_hex=$(echo "$raw_key" | tr -d '\\x')
900 echo -e "Verifying ciphertext with parameters:"
901 echo -e "\tcontents_encryption_mode: $contents_encryption_mode"
902 echo -e "\tfilenames_encryption_mode: $filenames_encryption_mode"
903 [ $# -ne 0 ] && echo -e "\toptions: $*"
905 _do_verify_ciphertext_for_encryption_policy \
906 "$contents_encryption_mode" \
907 "$filenames_encryption_mode" \
909 "$set_encpolicy_args" \
912 "$crypt_util_contents_args" \
913 "$crypt_util_filename_args"