This is my first time using git send-email,
hopefully everything's fine :)
PATCH 1/1 cmd_set_passphrase: initialize KDF parameters
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because bch_sb_crypt_init only configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters as bch_sb_crypt_init if the key wasn't encrypted before.
--- c_src/cmd_key.c | 9 +++++++-- c_src/crypto.c | 13 ++++++++----- c_src/crypto.h | 1 + 3 files changed, 16 insertions(+), 7 deletions(-)
c_src/cmd_key.c +7 -2
@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" );
@@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])die ("Error getting current key" );char *new_passphrase =read_passphrase_twice ("Enter new passphrase: " ); +if (!bch2_key_is_encrypted (&crypt->key)) { +bch_crypt_default_kdf_init (crypt); + } +struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (c->disk_sb.sb), +if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (sb), &new_key,sizeof (new_key)))die ("error encrypting key" ); crypt->key = new_key;
c_src/crypto.c +8 -5
@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb,get_random_bytes (&crypt->key.key,sizeof (crypt->key.key));if (passphrase) { - -SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); -SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); -SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); -SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +bch_crypt_default_kdf_init (crypt);struct bch_key passphrase_key =derive_passphrase (crypt, passphrase);
@@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb,memzero_explicit (&passphrase_key,sizeof (passphrase_key)); } } + +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { +SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); +SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); +SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); +SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +}
c_src/crypto.h +1 -0
@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *,void bch2_add_key (struct bch_sb *,const char *,const char *,const char *);void bch_sb_crypt_init (struct bch_sb *sb,struct bch_sb_field_crypt *,const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *);#endif /* _CRYPTO_H */
Signature · 1 line
2.45.2
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>
This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 77 lines
--- c_src/cmd_key.c | 9 +++++++-- c_src/crypto.c | 13 ++++++++----- c_src/crypto.h | 1 + 3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +7
-2@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" );@@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])die ("Error getting current key" );char *new_passphrase =read_passphrase_twice ("Enter new passphrase: " ); +if (!bch2_key_is_encrypted (&crypt->key)) { +bch_crypt_default_kdf_init (crypt); + } +struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (c->disk_sb.sb), +if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (sb), &new_key,sizeof (new_key)))die ("error encrypting key" ); crypt->key = new_key;c_src/crypto.c +8
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb,get_random_bytes (&crypt->key.key,sizeof (crypt->key.key));if (passphrase) { - -SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); -SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); -SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); -SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +bch_crypt_default_kdf_init (crypt);struct bch_key passphrase_key =derive_passphrase (crypt, passphrase);@@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb,memzero_explicit (&passphrase_key,sizeof (passphrase_key)); } } + +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { +SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); +SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); +SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); +SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *,void bch2_add_key (struct bch_sb *,const char *,const char *,const char *);void bch_sb_crypt_init (struct bch_sb *sb,struct bch_sb_field_crypt *,const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *);#endif /* _CRYPTO_H */ Signature · 1 line
2.45.2
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
Quoted 37 lines · Kent Overstreet
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 20 lines
--- c_src/cmd_key.c | 9 +++++++-- c_src/crypto.c | 13 ++++++++----- c_src/crypto.h | 1 + 3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +2
-1@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" );
Quoted 6 lines
c_src/cmd_key.c int cmd_set_passphrase(int argc, char *argv[]) · line 121
Quoted 6 lines
c_src/cmd_key.c +2
-0@@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])die ("Error getting current key" );char *new_passphrase =read_passphrase_twice ("Enter new passphrase: " ); +if (!bch2_key_is_encrypted (&crypt->key)) { +bch_crypt_default_kdf_init (crypt);
It works, but using bch_sb_crypt_init to reinitialize crypt may be better. In bch_sb_crypt_init, it has initialized the crypt and also cleaned new_passphrase.
Thanks
Hongbo
Quoted 53 lines
Quoted 51 lines
c_src/cmd_key.c +3
-1+ } +struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (c->disk_sb.sb), +if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (sb), &new_key,sizeof (new_key)))die ("error encrypting key" ); crypt->key = new_key;c_src/crypto.c +8
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb,get_random_bytes (&crypt->key.key,sizeof (crypt->key.key));if (passphrase) { - -SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); -SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); -SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); -SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +bch_crypt_default_kdf_init (crypt);struct bch_key passphrase_key =derive_passphrase (crypt, passphrase);@@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb,memzero_explicit (&passphrase_key,sizeof (passphrase_key)); } } + +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { +SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT ); +SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 )); +SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 )); +SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *,void bch2_add_key (struct bch_sb *,const char *,const char *,const char *);void bch_sb_crypt_init (struct bch_sb *sb,struct bch_sb_field_crypt *,const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *);#endif /* _CRYPTO_H */ Signature · 1 line
2.45.2
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
Quoted 45 lines · Hongbo Li
Quoted 32 lines · Kent Overstreet
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 15 lines
---
c_src/cmd_key.c| 9 +++++++--
c_src/crypto.c | 13 ++++++++-----
c_src/crypto.h | 1 +3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +1
-0@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb;Quoted 8 lines
Quoted 8 lines
c_src/cmd_key.c +1
-0+ struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("Filesystem does not have encryption enabled"); @@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])
die("Error getting current key");
char*new_passphrase=read_passphrase_twice("Enter new passphrase: "); + if (!bch2_key_is_encrypted(&crypt->key)) {
+bch_crypt_default_kdf_init(crypt);It works, but using
bch_sb_crypt_initto reinitialize crypt may be better. Inbch_sb_crypt_init, it has initialized the crypt and also cleanednew_passphrase.
Quoted 3 lines
Thanks
Hongbo
bch_sb_crypt_init also reinitializes the crypt->key, so I can't just reuse it here.
I could extract it into a bch_crypt_update_passphrase function. Originally I wanted to avoid doing that to limit the scope of this patchset, but I'll give it a shot
Thanks for the review!
Mae
Quoted 51 lines
Quoted 50 lines
Quoted 48 lines
+ }
+
structbch_keypassphrase_key=derive_passphrase(crypt, new_passphrase); - if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(c->disk_sb.sb), + if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb),
&new_key,sizeof(new_key)))
die("error encrypting key");
crypt->key=new_key;c_src/crypto.c +1
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb, get_random_bytes(&crypt->key.key, sizeof(crypt->key.key)); if (passphrase) { - - SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); - SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); - SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); - SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); + bch_crypt_default_kdf_init(crypt); struct bch_key passphrase_key = derive_passphrase(crypt, passphrase); @@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb, memzero_explicit(&passphrase_key, sizeof(passphrase_key)); }}
c_src/crypto.c +7
-0+ +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { + SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); + SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); + SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); + SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *, void bch2_add_key(struct bch_sb *, const char *, const char *, const char *); void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *, const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *); #endif /* _CRYPTO_H */Signature · 1 line
2.45.2
PATCH 1/1 Extract bch_crypt_update_passphrase function
This also fixes a bug where the set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase, due to bch_sb_crypt_init not configuring the KDF params if the passphrase wasn't specified during formatting.
--- c_src/cmd_key.c | 26 ++++++++++---------------- c_src/crypto.c | 43 ++++++++++++++++++++++++++++++++----------- c_src/crypto.h | 3 +++ 3 files changed, 45 insertions(+), 27 deletions(-)
c_src/cmd_key.c +10 -16
@@ -104,24 +104,19 @@ int cmd_set_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" ); -struct bch_encrypted_key new_key; - new_key.magic =BCH_KEY_MAGIC ; - -int ret =bch2_decrypt_sb_key (c, crypt, &new_key.key); +struct bch_key key; +int ret =bch2_decrypt_sb_key (c, crypt, &key);if (ret)die ("Error getting current key" );char *new_passphrase =read_passphrase_twice ("Enter new passphrase: " ); -struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (c->disk_sb.sb), - &new_key,sizeof (new_key))) -die ("error encrypting key" ); - crypt->key = new_key; +bch_crypt_update_passphrase (sb, crypt, &key, new_passphrase);bch2_revoke_key (c->disk_sb.sb);bch2_write_super (c);
@@ -142,18 +137,17 @@ int cmd_remove_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" ); -struct bch_encrypted_key new_key; - new_key.magic =BCH_KEY_MAGIC ; - -int ret =bch2_decrypt_sb_key (c, crypt, &new_key.key); +struct bch_key key; +int ret =bch2_decrypt_sb_key (c, crypt, &key);if (ret)die ("Error getting current key" ); - crypt->key = new_key; +bch_crypt_update_passphrase (sb, crypt, &key,NULL );bch2_write_super (c);bch2_fs_stop (c);
c_src/crypto.c +32 -11
58 lines not on this page · raw message
c_src/crypto.h +3 -0
@@ -19,4 +19,7 @@ void bch2_add_key(struct bch_sb *, const char *, const char *, const char *);void bch_sb_crypt_init (struct bch_sb *sb,struct bch_sb_field_crypt *,const char *); +void bch_crypt_update_passphrase (struct bch_sb *sb,struct bch_sb_field_crypt *crypt, +struct bch_key *key,const char *new_passphrase); +#endif /* _CRYPTO_H */
Signature · 1 line
2.45.2
Re: [PATCH] Extract bch_crypt_update_passphrase function
Quoted 93 lines · Mae Kasza
This also fixes a bug where the set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase, due to
bch_sb_crypt_initnot configuring the KDF params if the passphrase wasn't specified during formatting.Signed-off-by Mae Kasza <[email protected]>--- c_src/cmd_key.c | 26 ++++++++++---------------- c_src/crypto.c | 43 ++++++++++++++++++++++++++++++++----------- c_src/crypto.h | 3 +++ 3 files changed, 45 insertions(+), 27 deletions(-)c_src/cmd_key.c +10
-16@@ -104,24 +104,19 @@ int cmd_set_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" ); -struct bch_encrypted_key new_key; - new_key.magic =BCH_KEY_MAGIC ; - -int ret =bch2_decrypt_sb_key (c, crypt, &new_key.key); +struct bch_key key; +int ret =bch2_decrypt_sb_key (c, crypt, &key);if (ret)die ("Error getting current key" );char *new_passphrase =read_passphrase_twice ("Enter new passphrase: " ); -struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (c->disk_sb.sb), - &new_key,sizeof (new_key))) -die ("error encrypting key" ); - crypt->key = new_key; +bch_crypt_update_passphrase (sb, crypt, &key, new_passphrase);bch2_revoke_key (c->disk_sb.sb);bch2_write_super (c);@@ -142,18 +137,17 @@ int cmd_remove_passphrase(int argc, char *argv[])if (IS_ERR (c))die ("Error opening %s: %s" , argv[1 ],bch2_err_str (PTR_ERR (c))); -struct bch_sb_field_crypt *crypt =bch2_sb_field_get (c->disk_sb.sb, crypt); +struct bch_sb *sb = c->disk_sb.sb; +struct bch_sb_field_crypt *crypt =bch2_sb_field_get (sb, crypt);if (!crypt)die ("Filesystem does not have encryption enabled" ); -struct bch_encrypted_key new_key; - new_key.magic =BCH_KEY_MAGIC ; - -int ret =bch2_decrypt_sb_key (c, crypt, &new_key.key); +struct bch_key key; +int ret =bch2_decrypt_sb_key (c, crypt, &key);if (ret)die ("Error getting current key" ); - crypt->key = new_key; +bch_crypt_update_passphrase (sb, crypt, &key,NULL );bch2_write_super (c);bch2_fs_stop (c);c_src/crypto.c +9
-2@@ -176,26 +176,47 @@ void bch_sb_crypt_init(struct bch_sb *sb,struct bch_sb_field_crypt *crypt, const char *passphrase) { + struct bch_key key; +get_random_bytes (&key,sizeof (key)); + crypt->key.magic =BCH_KEY_MAGIC ; -get_random_bytes (&crypt->key.key,sizeof (crypt->key.key)); + crypt->key.key = key; -if (passphrase) { +bch_crypt_update_passphrase (sb, crypt, &key, passphrase); +} +void bch_crypt_update_passphrase( + struct bch_sb *sb, + struct bch_sb_field_crypt *crypt,
c_src/crypto.c void bch_sb_crypt_init(struct bch_sb *sb, · line 200
Quoted 10 lines
c_src/crypto.c +10
-0+struct bch_key *key, + const char *new_passphrase) +{ + + struct bch_encrypted_key new_key; + new_key.magic =BCH_KEY_MAGIC ; + new_key.key = *key; + +if (!new_passphrase) { + crypt->key = new_key;
May be the helper like this is needed. But we could assign the crypt->key outside, which seems more reasonable according to the helper name. Just in my humble opinion.
Thanks,
Hongbo
Quoted 43 lines
c_src/crypto.c +13
-9+return ; + } + +// If crypt already has an encrypted key reuse it's encryption params +if (!bch2_key_is_encrypted (&crypt->key)) {SET_BCH_CRYPT_KDF_TYPE (crypt,BCH_KDF_SCRYPT );SET_BCH_KDF_SCRYPT_N (crypt,ilog2 (16384 ));SET_BCH_KDF_SCRYPT_R (crypt,ilog2 (8 ));SET_BCH_KDF_SCRYPT_P (crypt,ilog2 (16 )); + } -struct bch_key passphrase_key =derive_passphrase (crypt, passphrase); - -assert (!bch2_key_is_encrypted (&crypt->key)); +struct bch_key passphrase_key =derive_passphrase (crypt, new_passphrase); -if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (sb), - &crypt->key,sizeof (crypt->key))) -die ("error encrypting key" ); +if (bch2_chacha_encrypt_key (&passphrase_key,__bch2_sb_key_nonce (sb), + &new_key,sizeof (new_key))) +die ("error encrypting key" ); -assert (bch2_key_is_encrypted (&crypt->key)); +memzero_explicit (&passphrase_key,sizeof (passphrase_key)); -memzero_explicit (&passphrase_key,sizeof (passphrase_key)); - } + crypt->key = new_key; +assert (bch2_key_is_encrypted (&crypt->key)); }c_src/crypto.h +3
-0@@ -19,4 +19,7 @@ void bch2_add_key(struct bch_sb *, const char *, const char *, const char *);void bch_sb_crypt_init (struct bch_sb *sb,struct bch_sb_field_crypt *,const char *); +void bch_crypt_update_passphrase (struct bch_sb *sb,struct bch_sb_field_crypt *crypt, +struct bch_key *key,const char *new_passphrase); +#endif /* _CRYPTO_H */
Re: [PATCH] Extract bch_crypt_update_passphrase function
Quoted 98 lines · Hongbo Li
Quoted 85 lines · Mae Kasza
This also fixes a bug where the set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase, due to
bch_sb_crypt_initnot configuring the KDF params if the passphrase wasn't specified during formatting.Signed-off-by Mae Kasza <[email protected]>---
c_src/cmd_key.c| 26 ++++++++++---------------- c_src/crypto.c | 43 ++++++++++++++++++++++++++++++++----------- c_src/crypto.h | 3 +++3 files changed, 45 insertions(+), 27 deletions(-)c_src/cmd_key.c +10
-10@@ -104,24 +104,19 @@ int cmd_set_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb; + struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("Filesystem does not have encryption enabled"); - struct bch_encrypted_key new_key; - new_key.magic = BCH_KEY_MAGIC; - - int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key); + struct bch_key key; + int ret = bch2_decrypt_sb_key(c, crypt, &key); if (ret) die("Error getting current key"); char *new_passphrase = read_passphrase_twice("Enter new passphrase: "); - struct bch_key passphrase_key = derive_passphrase(crypt, new_passphrase); - if (bch2_chacha_encrypt_key(&passphrase_key, __bch2_sb_key_nonce(c->disk_sb.sb), - &new_key, sizeof(new_key))) - die("error encrypting key"); - crypt->key = new_key; + bch_crypt_update_passphrase(sb, crypt, &key, new_passphrase); bch2_revoke_key(c->disk_sb.sb); bch2_write_super(c);@@ -142,18 +137,17 @@ int cmd_remove_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb; + struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("Filesystem does not have encryption enabled"); - struct bch_encrypted_key new_key; - new_key.magic = BCH_KEY_MAGIC; - - int ret = bch2_decrypt_sb_key(c, crypt, &new_key.key); + struct bch_key key; + int ret = bch2_decrypt_sb_key(c, crypt, &key); if (ret) die("Error getting current key"); - crypt->key = new_key; + bch_crypt_update_passphrase(sb, crypt, &key, NULL); bch2_write_super(c); bch2_fs_stop(c);c_src/crypto.c +6
-1@@ -176,26 +176,47 @@ void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *crypt, const char *passphrase) { + struct bch_key key; + get_random_bytes(&key, sizeof(key)); + crypt->key.magic = BCH_KEY_MAGIC; - get_random_bytes(&crypt->key.key, sizeof(crypt->key.key)); + crypt->key.key = key; - if (passphrase) { + bch_crypt_update_passphrase(sb, crypt, &key, passphrase); +}+void bch_crypt_update_passphrase(
+ structbch_sb*sb,
+ structbch_sb_field_crypt*crypt, + structbch_key*key,
+ const char*new_passphrase)c_src/crypto.c void bch_sb_crypt_init(struct bch_sb *sb, · line 200
Quoted 8 lines
c_src/crypto.c +8
-0+{ + + struct bch_encrypted_key new_key; + new_key.magic = BCH_KEY_MAGIC; + new_key.key = *key; + + if(!new_passphrase) { + crypt->key = new_key;May be the helper like this is needed. But we could assign the
crypt->keyoutside, which seems more reasonable according to the helper name. Just in my humble opinion.
Quoted 2 lines
Thanks,
Hongbo
We need to assign to crypt->key regardless, since it's what contains the key encrypted with passphrase, I don't really see why we would want to leave that up to the caller
Thanks,
Mae
Quoted 43 lines
Quoted 43 lines
+ return;
+ }
+
+ // If crypt already has an encrypted key reuse it's encryption params
+ if (!bch2_key_is_encrypted(&crypt->key)) {SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT);SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384));SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8));SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16));
+ }
- structbch_keypassphrase_key=derive_passphrase(crypt, passphrase); -
- assert(!bch2_key_is_encrypted(&crypt->key));
+ structbch_keypassphrase_key=derive_passphrase(crypt, new_passphrase);
- if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb), -&crypt->key,sizeof(crypt->key)))
-die("error encrypting key");
+ if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb),
+&new_key,sizeof(new_key)))
+die("error encrypting key");
- assert(bch2_key_is_encrypted(&crypt->key));
+memzero_explicit(&passphrase_key, sizeof(passphrase_key));
-memzero_explicit(&passphrase_key, sizeof(passphrase_key)); - }
+crypt->key=new_key;
+ assert(bch2_key_is_encrypted(&crypt->key));
}c_src/crypto.h +2
-0@@ -19,4 +19,7 @@ void bch2_add_key(struct bch_sb *, const char *, const char *, const char *); void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *, const char *); +void bch_crypt_update_passphrase(struct bch_sb *sb, struct bch_sb_field_crypt *crypt, + struct bch_key *key, const char *new_passphrase); +#endif /*
_CRYPTO_H*/
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
In reply to Mae KaszaQuoted 47 lines · Mae Kasza
Quoted 45 lines · Hongbo Li
Quoted 32 lines · Kent Overstreet
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 15 lines
---
c_src/cmd_key.c| 9 +++++++--
c_src/crypto.c | 13 ++++++++-----
c_src/crypto.h | 1 +3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +1
-0@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb;Quoted 8 lines
Quoted 8 lines
c_src/cmd_key.c +1
-0+ struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("Filesystem does not have encryption enabled"); @@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])
die("Error getting current key");
char*new_passphrase=read_passphrase_twice("Enter new passphrase: "); + if (!bch2_key_is_encrypted(&crypt->key)) {
+bch_crypt_default_kdf_init(crypt);It works, but using
bch_sb_crypt_initto reinitialize crypt may be better. Inbch_sb_crypt_init, it has initialized the crypt and also cleanednew_passphrase.
Quoted 5 lines
Quoted 3 lines
Thanks
Hongbo
bch_sb_crypt_initalso reinitializes thecrypt->key, so I can't just reuse it here.
Yeah, it will change crypt->key. It seems the main point is not clean the password.
Do we need keep the crypt->key even the password is changed? Or does the crypt->key like a random seed and need to be kept in the whole lifecycle? Sorry I am a bit confused about this.
Thanks,
Hongbo
Quoted 57 lines
I could extract it into a
bch_crypt_update_passphrasefunction. Originally I wanted to avoid doing that to limit the scope of this patchset, but I'll give it a shotThanks for the review!
Mae
Quoted 51 lines
Quoted 50 lines
Quoted 48 lines
+ }
+
structbch_keypassphrase_key=derive_passphrase(crypt, new_passphrase); - if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(c->disk_sb.sb), + if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb),
&new_key,sizeof(new_key)))
die("error encrypting key");
crypt->key=new_key;c_src/crypto.c +1
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb, get_random_bytes(&crypt->key.key, sizeof(crypt->key.key)); if (passphrase) { - - SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); - SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); - SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); - SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); + bch_crypt_default_kdf_init(crypt); struct bch_key passphrase_key = derive_passphrase(crypt, passphrase); @@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb, memzero_explicit(&passphrase_key, sizeof(passphrase_key)); }}
c_src/crypto.c +7
-0+ +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { + SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); + SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); + SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); + SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *, void bch2_add_key(struct bch_sb *, const char *, const char *, const char *); void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *, const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *); #endif /* _CRYPTO_H */Signature · 1 line
2.45.2
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
Quoted 53 lines · Hongbo Li
Quoted 50 lines · Mae Kasza
Quoted 48 lines · Hongbo Li
Quoted 32 lines · Kent Overstreet
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 15 lines
---
c_src/cmd_key.c| 9 +++++++--
c_src/crypto.c | 13 ++++++++-----
c_src/crypto.h | 1 +3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +1
-0@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb;Quoted 8 lines
Quoted 8 lines
c_src/cmd_key.c +1
-0+ struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("Filesystem does not have encryption enabled"); @@ -116,9 +117,13 @@ int cmd_set_passphrase(int argc, char *argv[])
die("Error getting current key");
char*new_passphrase=read_passphrase_twice("Enter new passphrase: "); + if (!bch2_key_is_encrypted(&crypt->key)) {
+bch_crypt_default_kdf_init(crypt);It works, but using
bch_sb_crypt_initto reinitialize crypt may be better. Inbch_sb_crypt_init, it has initialized the crypt and also cleanednew_passphrase.Thanks
Hongbo
Quoted 7 lines
Quoted 2 lines
bch_sb_crypt_initalso reinitializes thecrypt->key, so I can't just reuse it here.Yeah, it will change
crypt->key. It seems the main point is not clean the password.
Do we need keep thecrypt->keyeven the password is changed? Or does thecrypt->keylike a random seed and need to be kept in the whole lifecycle? Sorry I am a bit confused about this.Thanks,
Hongbo
crypt->key does need to stay constant for the entire lifetime of a partition, since that's what's actually used to encrypt the data on the partition. The passphrase is just used to encrypt the crypt->key
Thanks,
Mae
Quoted 58 lines
Quoted 58 lines
I could extract it into a
bch_crypt_update_passphrasefunction. Originally I wanted to avoid doing that to limit the scope of this patchset, but I'll give it a shotThanks for the review!
Mae
Quoted 51 lines
Quoted 50 lines
Quoted 48 lines
+ }
+
structbch_keypassphrase_key=derive_passphrase(crypt, new_passphrase); - if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(c->disk_sb.sb), + if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb),
&new_key,sizeof(new_key)))
die("error encrypting key");
crypt->key=new_key;c_src/crypto.c +1
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb, get_random_bytes(&crypt->key.key, sizeof(crypt->key.key)); if (passphrase) { - - SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); - SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); - SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); - SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); + bch_crypt_default_kdf_init(crypt); struct bch_key passphrase_key = derive_passphrase(crypt, passphrase); @@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb, memzero_explicit(&passphrase_key, sizeof(passphrase_key)); }}
c_src/crypto.c +7
-0+ +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { + SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); + SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); + SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); + SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *, void bch2_add_key(struct bch_sb *, const char *, const char *, const char *); void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *, const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *); #endif /* _CRYPTO_H */Signature · 1 line
2.45.2
Re: [PATCH] cmd_set_passphrase: initialize KDF parameters
Hi,
Appologies if this is bad etiquette, but I haven't gotten any review feedback and it seems like fixing this is relatively important to users(https://github.com/koverstreet/bcachefs-tools/pull/329#issuecomment-2336787443). Any chance someone(Kent) could look at this and merge the patch?
Thanks,
Mae
Quoted 128 lines · Mae Kasza
Quoted 54 lines · Hongbo Li
Quoted 51 lines · Mae Kasza
Quoted 49 lines · Hongbo Li
Quoted 33 lines · Kent Overstreet
Quoted 11 lines · Mae Kasza
From: mae <[email protected]>
The set-passphrase command failed to derive the key for disks initially formatted with --encrypted and --no_passphrase.
This happened because
bch_sb_crypt_initonly configures the KDF params if a passphrase is specified.
This commit makes the command initialize the KDF with the same parameters asbch_sb_crypt_initif the key wasn't encrypted before.Signed-off-by Mae Kasza <[email protected]>This looks good to me, but I'm in the middle of a 20 hour drive and not feeling super confident to review crypto stuff tonight, so maybe someone else can go over it too before I pull it in..
Quoted 16 lines
---
c_src/cmd_key.c| 9 +++++++--
c_src/crypto.c | 13 ++++++++-----
c_src/crypto.h | 1 +3 files changed, 16 insertions(+), 7 deletions(-)c_src/cmd_key.c +2
-0@@ -104,7 +104,8 @@ int cmd_set_passphrase(int argc, char *argv[]) if (IS_ERR(c)) die("Error opening %s: %s", argv[1], bch2_err_str(PTR_ERR(c))); - struct bch_sb_field_crypt *crypt = bch2_sb_field_get(c->disk_sb.sb, crypt); + struct bch_sb *sb = c->disk_sb.sb; + struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb,Quoted 8 lines
Quoted 8 lines
c_src/cmd_key.c +0
-0crypt); if (!crypt) die("Filesystem does not have encryption enabled");@@ -116,9 +117,13 @@ int
cmd_set_passphrase(int argc, char *argv[])die("Error getting current key");
char*new_passphrase=read_passphrase_twice("Enter new passphrase: "); + if (!bch2_key_is_encrypted(&crypt->key)) {
+bch_crypt_default_kdf_init(crypt);It works, but using
bch_sb_crypt_initto reinitialize crypt may be better. Inbch_sb_crypt_init, it has initialized the crypt and also cleanednew_passphrase.Thanks
HongboQuoted 7 lines
Quoted 2 lines
bch_sb_crypt_initalso reinitializes thecrypt->key, so I can't just reuse it here.Yeah, it will change
crypt->key. It seems the main point is not clean the password.
Do we need keep thecrypt->keyeven the password is changed? Or does thecrypt->keylike a random seed and need to be kept in the whole lifecycle? Sorry I am a bit confused about this.Thanks,
Hongbo
crypt->keydoes need to stay constant for the entire lifetime of a partition, since that's what's actually used to encrypt the data on the partition. The passphrase is just used to encrypt thecrypt->keyThanks,
Mae
Quoted 58 lines
Quoted 58 lines
I could extract it into a
bch_crypt_update_passphrasefunction. Originally I wanted to avoid doing that to limit the scope of this patchset, but I'll give it a shotThanks for the review!
Mae
Quoted 51 lines
Quoted 50 lines
Quoted 48 lines
+ }
+
structbch_keypassphrase_key=derive_passphrase(crypt, new_passphrase); - if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(c->disk_sb.sb), + if (bch2_chacha_encrypt_key(&passphrase_key,__bch2_sb_key_nonce(sb),
&new_key,sizeof(new_key)))
die("error encrypting key");
crypt->key=new_key;c_src/crypto.c +1
-5@@ -180,11 +180,7 @@ void bch_sb_crypt_init(struct bch_sb *sb, get_random_bytes(&crypt->key.key, sizeof(crypt->key.key)); if (passphrase) { - - SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); - SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); - SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); - SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); + bch_crypt_default_kdf_init(crypt); struct bch_key passphrase_key = derive_passphrase(crypt, passphrase); @@ -199,3 +195,10 @@ void bch_sb_crypt_init(struct bch_sb *sb, memzero_explicit(&passphrase_key, sizeof(passphrase_key)); }}
c_src/crypto.c +7
-0+ +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *crypt) { + SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); + SET_BCH_KDF_SCRYPT_N(crypt, ilog2(16384)); + SET_BCH_KDF_SCRYPT_R(crypt, ilog2(8)); + SET_BCH_KDF_SCRYPT_P(crypt, ilog2(16)); +}c_src/crypto.h +1
-0@@ -18,5 +18,6 @@ void bch2_passphrase_check(struct bch_sb *, const char *, void bch2_add_key(struct bch_sb *, const char *, const char *, const char *); void bch_sb_crypt_init(struct bch_sb *sb, struct bch_sb_field_crypt *, const char *); +void bch_crypt_default_kdf_init (struct bch_sb_field_crypt *); #endif /* _CRYPTO_H */Signature · 1 line
2.45.2