common: convert to SPDX license tags
[xfstests-dev.git] / common / encrypt
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2016 Google, Inc.  All Rights Reserved.
4 #
5 # Functions for setting up and testing file encryption
6
7 _require_scratch_encryption()
8 {
9         _require_scratch
10
11         _require_xfs_io_command "set_encpolicy"
12
13         # The 'test_dummy_encryption' mount option interferes with trying to use
14         # encryption for real, even if we are just trying to get/set policies
15         # and never put any keys in the keyring.  So skip the real encryption
16         # tests if the 'test_dummy_encryption' mount option was specified.
17         _exclude_scratch_mount_option "test_dummy_encryption"
18
19         # Make a filesystem on the scratch device with the encryption feature
20         # enabled.  If this fails then probably the userspace tools (e.g.
21         # e2fsprogs or f2fs-tools) are too old to understand encryption.
22         if ! _scratch_mkfs_encrypted &>>$seqres.full; then
23                 _notrun "$FSTYP userspace tools do not support encryption"
24         fi
25
26         # Try to mount the filesystem.  If this fails then either the kernel
27         # isn't aware of encryption, or the mkfs options were not compatible
28         # with encryption (e.g. ext4 with block size != PAGE_SIZE).
29         if ! _try_scratch_mount &>>$seqres.full; then
30                 _notrun "kernel is unaware of $FSTYP encryption feature," \
31                         "or mkfs options are not compatible with encryption"
32         fi
33
34         # The kernel may be aware of encryption without supporting it.  For
35         # example, for ext4 this is the case with kernels configured with
36         # CONFIG_EXT4_FS_ENCRYPTION=n.  Detect support for encryption by trying
37         # to set an encryption policy.  (For ext4 we could instead check for the
38         # presence of /sys/fs/ext4/features/encryption, but this is broken on
39         # some older kernels and is ext4-specific anyway.)
40         mkdir $SCRATCH_MNT/tmpdir
41         if $XFS_IO_PROG -c set_encpolicy $SCRATCH_MNT/tmpdir \
42                 2>&1 >>$seqres.full | \
43                 egrep -q 'Inappropriate ioctl for device|Operation not supported'
44         then
45                 _notrun "kernel does not support $FSTYP encryption"
46         fi
47         rmdir $SCRATCH_MNT/tmpdir
48         _scratch_unmount
49 }
50
51 _scratch_mkfs_encrypted()
52 {
53         case $FSTYP in
54         ext4|f2fs)
55                 _scratch_mkfs -O encrypt
56                 ;;
57         ubifs)
58                 # erase the UBI volume; reformated automatically on next mount
59                 $UBIUPDATEVOL_PROG ${SCRATCH_DEV} -t
60                 ;;
61         *)
62                 _notrun "No encryption support for $FSTYP"
63                 ;;
64         esac
65 }
66
67 _scratch_mkfs_sized_encrypted()
68 {
69         case $FSTYP in
70         ext4|f2fs)
71                 MKFS_OPTIONS="$MKFS_OPTIONS -O encrypt" _scratch_mkfs_sized $*
72                 ;;
73         *)
74                 _notrun "Filesystem $FSTYP not supported in _scratch_mkfs_sized_encrypted"
75                 ;;
76         esac
77 }
78
79 # Give the invoking shell a new session keyring.  This makes any keys we add to
80 # the session keyring scoped to the lifetime of the test script.
81 _new_session_keyring()
82 {
83         $KEYCTL_PROG new_session >>$seqres.full
84 }
85
86 # Generate a key descriptor (16 character hex string)
87 _generate_key_descriptor()
88 {
89         local keydesc=""
90         local i
91         for ((i = 0; i < 8; i++)); do
92                 keydesc="${keydesc}$(printf "%02x" $(( $RANDOM % 256 )))"
93         done
94         echo $keydesc
95 }
96
97 # Generate a raw encryption key, but don't add it to the keyring yet.
98 _generate_raw_encryption_key()
99 {
100         local raw=""
101         local i
102         for ((i = 0; i < 64; i++)); do
103                 raw="${raw}\\x$(printf "%02x" $(( $RANDOM % 256 )))"
104         done
105         echo $raw
106 }
107
108 # Add the specified raw encryption key to the session keyring, using the
109 # specified key descriptor.
110 _add_encryption_key()
111 {
112         local keydesc=$1
113         local raw=$2
114
115         #
116         # Add the key to the session keyring.  The required structure is:
117         #
118         #       #define FS_MAX_KEY_SIZE 64
119         #       struct fscrypt_key {
120         #               u32 mode;
121         #               u8 raw[FS_MAX_KEY_SIZE];
122         #               u32 size;
123         #       } __packed;
124         #
125         # The kernel ignores 'mode' but requires that 'size' be 64.
126         #
127         # Keys are named $FSTYP:KEYDESC where KEYDESC is the 16-character key
128         # descriptor hex string.  Newer kernels (ext4 4.8 and later, f2fs 4.6
129         # and later) also allow the common key prefix "fscrypt:" in addition to
130         # their filesystem-specific key prefix ("ext4:", "f2fs:").  It would be
131         # nice to use the common key prefix, but for now use the filesystem-
132         # specific prefix to make it possible to test older kernels...
133         #
134         local big_endian=$(echo -ne '\x11' | od -tx2 | head -1 | \
135                            cut -f2 -d' ' | cut -c1 )
136         if (( big_endian )); then
137                 local mode='\x00\x00\x00\x00'
138                 local size='\x00\x00\x00\x40'
139         else
140                 local mode='\x00\x00\x00\x00'
141                 local size='\x40\x00\x00\x00'
142         fi
143         echo -n -e "${mode}${raw}${size}" |
144                 $KEYCTL_PROG padd logon $FSTYP:$keydesc @s >>$seqres.full
145 }
146
147 #
148 # Generate a random encryption key, add it to the session keyring, and print out
149 # the resulting key descriptor (example: "8bf798e1a494e1ec").  Requires the
150 # keyctl program.  It's assumed the caller has already set up a test-scoped
151 # session keyring using _new_session_keyring.
152 #
153 _generate_encryption_key()
154 {
155         local keydesc=$(_generate_key_descriptor)
156         local raw=$(_generate_raw_encryption_key)
157
158         _add_encryption_key $keydesc $raw
159
160         echo $keydesc
161 }
162
163 # Unlink an encryption key from the session keyring, given its key descriptor.
164 _unlink_encryption_key()
165 {
166         local keydesc=$1
167         local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
168         $KEYCTL_PROG unlink $keyid >>$seqres.full
169 }
170
171 # Revoke an encryption key from the keyring, given its key descriptor.
172 _revoke_encryption_key()
173 {
174         local keydesc=$1
175         local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
176         $KEYCTL_PROG revoke $keyid >>$seqres.full
177 }