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