1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Google, Inc. All Rights Reserved.
4 * Author: Eric Biggers <ebiggers@google.com>
7 #include "platform_defs.h"
10 #include "libfrog/paths.h"
14 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18 * We may have to declare the fscrypt ioctls ourselves because someone may be
19 * compiling xfsprogs with old kernel headers. And since some old versions of
20 * <linux/fs.h> declared the policy struct and ioctl numbers but not the flags
21 * and modes, our declarations must be split into two conditional blocks.
24 /* Policy struct and ioctl numbers */
25 #ifndef FS_IOC_SET_ENCRYPTION_POLICY
26 #define FS_KEY_DESCRIPTOR_SIZE 8
28 struct fscrypt_policy
{
30 __u8 contents_encryption_mode
;
31 __u8 filenames_encryption_mode
;
33 __u8 master_key_descriptor
[FS_KEY_DESCRIPTOR_SIZE
];
34 } __attribute__((packed
));
36 #define FS_IOC_SET_ENCRYPTION_POLICY _IOR('f', 19, struct fscrypt_policy)
37 #define FS_IOC_GET_ENCRYPTION_PWSALT _IOW('f', 20, __u8[16])
38 #define FS_IOC_GET_ENCRYPTION_POLICY _IOW('f', 21, struct fscrypt_policy)
39 #endif /* FS_IOC_SET_ENCRYPTION_POLICY */
41 /* Policy flags and encryption modes */
42 #ifndef FS_ENCRYPTION_MODE_AES_256_XTS
43 #define FS_POLICY_FLAGS_PAD_4 0x00
44 #define FS_POLICY_FLAGS_PAD_8 0x01
45 #define FS_POLICY_FLAGS_PAD_16 0x02
46 #define FS_POLICY_FLAGS_PAD_32 0x03
47 #define FS_POLICY_FLAGS_PAD_MASK 0x03
48 #define FS_POLICY_FLAGS_VALID 0x03
50 #define FS_ENCRYPTION_MODE_INVALID 0
51 #define FS_ENCRYPTION_MODE_AES_256_XTS 1
52 #define FS_ENCRYPTION_MODE_AES_256_GCM 2
53 #define FS_ENCRYPTION_MODE_AES_256_CBC 3
54 #define FS_ENCRYPTION_MODE_AES_256_CTS 4
55 #endif /* FS_ENCRYPTION_MODE_AES_256_XTS */
57 static cmdinfo_t get_encpolicy_cmd
;
58 static cmdinfo_t set_encpolicy_cmd
;
61 set_encpolicy_help(void)
65 " assign an encryption policy to the currently open file\n"
68 " 'set_encpolicy' - assign policy with default key [0000000000000000]\n"
69 " 'set_encpolicy 0000111122223333' - assign policy with specified key\n"
71 " -c MODE -- contents encryption mode\n"
72 " -n MODE -- filenames encryption mode\n"
73 " -f FLAGS -- policy flags\n"
74 " -v VERSION -- version of policy structure\n"
76 " MODE can be numeric or one of the following predefined values:\n"
77 " AES-256-XTS, AES-256-CTS, AES-256-GCM, AES-256-CBC\n"
78 " FLAGS and VERSION must be numeric.\n"
80 " Note that it's only possible to set an encryption policy on an empty\n"
81 " directory. It's then inherited by new files and subdirectories.\n"
88 } available_modes
[] = {
89 {FS_ENCRYPTION_MODE_AES_256_XTS
, "AES-256-XTS"},
90 {FS_ENCRYPTION_MODE_AES_256_CTS
, "AES-256-CTS"},
91 {FS_ENCRYPTION_MODE_AES_256_GCM
, "AES-256-GCM"},
92 {FS_ENCRYPTION_MODE_AES_256_CBC
, "AES-256-CBC"},
96 parse_byte_value(const char *arg
, __u8
*value_ret
)
101 value
= strtol(arg
, &tmp
, 0);
102 if (value
< 0 || value
> 255 || tmp
== arg
|| *tmp
!= '\0')
109 parse_mode(const char *arg
, __u8
*mode_ret
)
113 for (i
= 0; i
< ARRAY_SIZE(available_modes
); i
++) {
114 if (strcmp(arg
, available_modes
[i
].name
) == 0) {
115 *mode_ret
= available_modes
[i
].mode
;
120 return parse_byte_value(arg
, mode_ret
);
129 for (i
= 0; i
< ARRAY_SIZE(available_modes
); i
++)
130 if (mode
== available_modes
[i
].mode
)
131 return available_modes
[i
].name
;
133 sprintf(buf
, "0x%02x", mode
);
138 keydesc2str(__u8 master_key_descriptor
[FS_KEY_DESCRIPTOR_SIZE
])
140 static char buf
[2 * FS_KEY_DESCRIPTOR_SIZE
+ 1];
143 for (i
= 0; i
< FS_KEY_DESCRIPTOR_SIZE
; i
++)
144 sprintf(&buf
[2 * i
], "%02x", master_key_descriptor
[i
]);
150 get_encpolicy_f(int argc
, char **argv
)
152 struct fscrypt_policy policy
;
154 if (ioctl(file
->fd
, FS_IOC_GET_ENCRYPTION_POLICY
, &policy
) < 0) {
155 fprintf(stderr
, "%s: failed to get encryption policy: %s\n",
156 file
->name
, strerror(errno
));
161 printf("Encryption policy for %s:\n", file
->name
);
162 printf("\tPolicy version: %u\n", policy
.version
);
163 printf("\tMaster key descriptor: %s\n",
164 keydesc2str(policy
.master_key_descriptor
));
165 printf("\tContents encryption mode: %u (%s)\n",
166 policy
.contents_encryption_mode
,
167 mode2str(policy
.contents_encryption_mode
));
168 printf("\tFilenames encryption mode: %u (%s)\n",
169 policy
.filenames_encryption_mode
,
170 mode2str(policy
.filenames_encryption_mode
));
171 printf("\tFlags: 0x%02x\n", policy
.flags
);
176 set_encpolicy_f(int argc
, char **argv
)
179 struct fscrypt_policy policy
;
181 /* Initialize the policy structure with default values */
182 memset(&policy
, 0, sizeof(policy
));
183 policy
.contents_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_XTS
;
184 policy
.filenames_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_CTS
;
185 policy
.flags
= FS_POLICY_FLAGS_PAD_16
;
188 while ((c
= getopt(argc
, argv
, "c:n:f:v:")) != EOF
) {
191 if (!parse_mode(optarg
,
192 &policy
.contents_encryption_mode
)) {
193 fprintf(stderr
, "invalid contents encryption "
194 "mode: %s\n", optarg
);
199 if (!parse_mode(optarg
,
200 &policy
.filenames_encryption_mode
)) {
201 fprintf(stderr
, "invalid filenames encryption "
202 "mode: %s\n", optarg
);
207 if (!parse_byte_value(optarg
, &policy
.flags
)) {
208 fprintf(stderr
, "invalid flags: %s\n", optarg
);
213 if (!parse_byte_value(optarg
, &policy
.version
)) {
214 fprintf(stderr
, "invalid policy version: %s\n",
220 return command_usage(&set_encpolicy_cmd
);
227 return command_usage(&set_encpolicy_cmd
);
229 /* Parse key descriptor if specified */
231 const char *keydesc
= argv
[0];
233 unsigned long long x
;
236 if (strlen(keydesc
) != FS_KEY_DESCRIPTOR_SIZE
* 2) {
237 fprintf(stderr
, "invalid key descriptor: %s\n",
242 x
= strtoull(keydesc
, &tmp
, 16);
243 if (tmp
== keydesc
|| *tmp
!= '\0') {
244 fprintf(stderr
, "invalid key descriptor: %s\n",
249 for (i
= 0; i
< FS_KEY_DESCRIPTOR_SIZE
; i
++) {
250 policy
.master_key_descriptor
[i
] = x
>> 56;
255 /* Set the encryption policy */
256 if (ioctl(file
->fd
, FS_IOC_SET_ENCRYPTION_POLICY
, &policy
) < 0) {
257 fprintf(stderr
, "%s: failed to set encryption policy: %s\n",
258 file
->name
, strerror(errno
));
269 get_encpolicy_cmd
.name
= "get_encpolicy";
270 get_encpolicy_cmd
.cfunc
= get_encpolicy_f
;
271 get_encpolicy_cmd
.argmin
= 0;
272 get_encpolicy_cmd
.argmax
= 0;
273 get_encpolicy_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
274 get_encpolicy_cmd
.oneline
=
275 _("display the encryption policy of the current file");
277 set_encpolicy_cmd
.name
= "set_encpolicy";
278 set_encpolicy_cmd
.cfunc
= set_encpolicy_f
;
279 set_encpolicy_cmd
.args
=
280 _("[-c mode] [-n mode] [-f flags] [-v version] [keydesc]");
281 set_encpolicy_cmd
.argmin
= 0;
282 set_encpolicy_cmd
.argmax
= -1;
283 set_encpolicy_cmd
.flags
= CMD_NOMAP_OK
| CMD_FOREIGN_OK
;
284 set_encpolicy_cmd
.oneline
=
285 _("assign an encryption policy to the current file");
286 set_encpolicy_cmd
.help
= set_encpolicy_help
;
288 add_command(&get_encpolicy_cmd
);
289 add_command(&set_encpolicy_cmd
);