]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_key.c
e670b508a15c02587be1d6abfd113e24bf0a0111
[bcachefs-tools-debian] / cmd_key.c
1 #include <errno.h>
2 #include <unistd.h>
3 #include <uuid/uuid.h>
4
5 #include "cmds.h"
6 #include "libbcachefs/checksum.h"
7 #include "crypto.h"
8 #include "libbcachefs.h"
9
10 int cmd_unlock(int argc, char *argv[])
11 {
12         struct bch_opts opts = bch2_opts_empty();
13         struct bch_sb_handle sb;
14         char *passphrase;
15
16         if (argc != 2)
17                 die("Please supply a single device");
18
19         int ret = bch2_read_super(argv[1], &opts, &sb);
20         if (ret)
21                 die("Error opening %s: %s", argv[1], strerror(-ret));
22
23         passphrase = read_passphrase("Enter passphrase: ");
24
25         bch2_add_key(sb.sb, passphrase);
26
27         memzero_explicit(passphrase, strlen(passphrase));
28         free(passphrase);
29         return 0;
30 }
31
32 int cmd_set_passphrase(int argc, char *argv[])
33 {
34         struct bch_opts opts = bch2_opts_empty();
35         struct bch_fs *c;
36
37         if (argc < 2)
38                 die("Please supply one or more devices");
39
40         opt_set(opts, nostart, true);
41         c = bch2_fs_open(argv + 1, argc - 1, opts);
42         if (IS_ERR(c))
43                 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
44
45         struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb);
46         if (!crypt)
47                 die("Filesystem does not have encryption enabled");
48
49         struct bch_encrypted_key new_key;
50         new_key.magic = BCH_KEY_MAGIC;
51
52         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
53         if (ret)
54                 die("Error getting current key");
55
56         char *new_passphrase = read_passphrase_twice("Enter new passphrase: ");
57         struct bch_key passphrase_key = derive_passphrase(crypt, new_passphrase);
58
59         if (bch2_chacha_encrypt_key(&passphrase_key, __bch2_sb_key_nonce(c->disk_sb),
60                                     &new_key, sizeof(new_key)))
61                 die("error encrypting key");
62         crypt->key = new_key;
63
64         bch2_write_super(c);
65         bch2_fs_stop(c);
66         return 0;
67 }
68
69 int cmd_remove_passphrase(int argc, char *argv[])
70 {
71         struct bch_opts opts = bch2_opts_empty();
72         struct bch_fs *c;
73
74         if (argc < 2)
75                 die("Please supply one or more devices");
76
77         opt_set(opts, nostart, true);
78         c = bch2_fs_open(argv + 1, argc - 1, opts);
79         if (IS_ERR(c))
80                 die("Error opening %s: %s", argv[1], strerror(-PTR_ERR(c)));
81
82         struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(c->disk_sb);
83         if (!crypt)
84                 die("Filesystem does not have encryption enabled");
85
86         struct bch_encrypted_key new_key;
87         new_key.magic = BCH_KEY_MAGIC;
88
89         int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key);
90         if (ret)
91                 die("Error getting current key");
92
93         crypt->key = new_key;
94
95         bch2_write_super(c);
96         bch2_fs_stop(c);
97         return 0;
98 }