5 #include <nih/command.h>
6 #include <nih/option.h>
12 NihOption opts_unlock[] = {
16 int cmd_unlock(NihCommand *command, char * const *args)
18 struct bcache_disk_key disk_key;
19 struct bcache_key key;
25 if (!args[0] || args[1])
26 die("please supply a single device");
28 bcache_super_read(args[0], &sb);
30 if (!CACHE_SET_ENCRYPTION_KEY(&sb))
31 die("filesystem is not encrypted");
33 memcpy(&disk_key, sb.encryption_key, sizeof(disk_key));
35 if (!memcmp(&disk_key, bch_key_header, sizeof(bch_key_header)))
36 die("filesystem does not have encryption key");
38 passphrase = read_passphrase("Enter passphrase: ");
40 derive_passphrase(&key, passphrase);
41 disk_key_encrypt(&disk_key, &key);
43 if (memcmp(&disk_key, bch_key_header, sizeof(bch_key_header)))
44 die("incorrect passphrase");
46 uuid_unparse_lower(sb.user_uuid.b, uuid);
47 sprintf(description, "bcache:%s", uuid);
49 if (add_key("logon", description, &key, sizeof(key),
50 KEY_SPEC_USER_KEYRING) < 0)
51 die("add_key error: %s", strerror(errno));
53 memzero_explicit(&disk_key, sizeof(disk_key));
54 memzero_explicit(&key, sizeof(key));
55 memzero_explicit(passphrase, strlen(passphrase));