Skip to content

Commit d4cdfb7

Browse files
committed
tpm2_control/tpm2d_control: allow setting of key_len for FDE
Expose paramter --key_len | -l to dmcrypt_setup. This allows to truncate the key to AES128 or AES192. The tpm2d internally always generates an FDE key of CRYPTFS_FDE_KEY_LEN which is 512 bit random data for aes-xts with AES256. Signed-off-by: Michael Weiß <[email protected]>
1 parent ab7a1e5 commit d4cdfb7

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

tpm2_control/tpm2d_control.c

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,18 @@ print_usage(const char *cmd)
4848
printf("Usage: %s [-s <socket file>] <command> [<command args>]\n", cmd);
4949
printf("\n");
5050
printf("commands:\n");
51-
printf("\tdmcrypt_setup <device path> <passwd>\n\t\tSetup device mapper with tpm2d's internal disk encryption key, password for corresponding nvindex\n");
52-
printf("\tdmcrypt_lock <passwd>\n\t\tLocks further dmsetup attampts by locking tpm2d's internal disk encryption key, password for corresponding nvindex\n");
51+
printf("\tdmcrypt_setup [-l|--key_len <len>] <device path> [<passwd>]\n"
52+
"\t\tSetup device mapper with tpm2d's internal disk encryption key,\n"
53+
"\t\tpassword for corresponding nvindex,\n"
54+
"\t\tif -l is set, use len bytes of nvindex as key\n");
55+
printf("\tdmcrypt_lock <passwd>\n"
56+
"\t\tLocks further dmsetup attampts by locking tpm2d's internal disk encryption key,\n"
57+
"\t\tpassword for corresponding nvindex\n");
5358
printf("\texit\n\t\tStop TPM2D daemon\n");
54-
printf("\tgetrandom <size>\n\t\tRequest some random date of size size from TPM\n");
59+
printf("\tgetrandom <size>\n\t\tRequest some random data of size size from TPM\n");
5560
printf("\tclear <passwd>\n\t\tClear TPM using lockout password\n");
56-
printf("\tchange_owner <passwd> <new passwd>\n\t\tiChanges the password for the owner hierarchy of the TPM\n");
61+
printf("\tchange_owner <passwd> <new passwd>\n"
62+
"\t\tChanges the password for the owner hierarchy of the TPM\n");
5763
printf("\n");
5864
exit(-1);
5965
}
@@ -92,6 +98,27 @@ static const struct option global_options[] = { { "socket", required_argument, 0
9298
{ "help", no_argument, 0, 'h' },
9399
{ 0, 0, 0, 0 } };
94100

101+
static const struct option dmsetup_options[] = { { "key_len", required_argument, 0, 'l' },
102+
{ 0, 0, 0, 0 } };
103+
104+
static ControllerToTpm__FdeKeyType
105+
get_fde_key_type(int len)
106+
{
107+
INFO("Get FdeKeyType for len: %d", len);
108+
109+
switch (len) {
110+
case 32:
111+
return CONTROLLER_TO_TPM__FDE_KEY_TYPE__AES128;
112+
case 48:
113+
return CONTROLLER_TO_TPM__FDE_KEY_TYPE__AES192;
114+
case 64:
115+
return CONTROLLER_TO_TPM__FDE_KEY_TYPE__AES256;
116+
default:
117+
INFO("Unsupported len %d for FdeKeyType, using default (AES256)", len);
118+
return CONTROLLER_TO_TPM__FDE_KEY_TYPE__AES256;
119+
}
120+
}
121+
95122
int
96123
main(int argc, char *argv[])
97124
{
@@ -116,8 +143,10 @@ main(int argc, char *argv[])
116143
}
117144

118145
// need at least one more argument (i.e. command string)
119-
if (optind >= argc)
146+
if (optind >= argc) {
147+
INFO("need at least one more argument (i.e. command string)");
120148
print_usage(argv[0]);
149+
}
121150

122151
// build ControllerToTpm message
123152
ControllerToTpm msg = CONTROLLER_TO_TPM__INIT;
@@ -129,6 +158,25 @@ main(int argc, char *argv[])
129158
if (optind >= argc)
130159
print_usage(argv[0]);
131160

161+
optind--;
162+
char **dm_argv = &argv[optind];
163+
int dm_argc = argc - optind;
164+
optind = 0; // reset optind to scan command-specific options
165+
for (int c, option_index = 0;
166+
- 1 !=
167+
(c = getopt_long(dm_argc, dm_argv, "+l:", dmsetup_options, &option_index));) {
168+
switch (c) {
169+
case 'l':
170+
msg.has_dmcrypt_key_type = true;
171+
msg.dmcrypt_key_type = get_fde_key_type(atoi(optarg));
172+
break;
173+
default:
174+
print_usage(argv[0]);
175+
ASSERT(false); // never reached
176+
}
177+
}
178+
optind += argc - dm_argc; // adjust optind to be used with argv
179+
132180
msg.dmcrypt_device = argv[optind++];
133181
if (optind < argc)
134182
msg.password = argv[optind++];

0 commit comments

Comments
 (0)