X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=crypto.c;h=32671bd84c332e90deb0510743443faf41df6c45;hb=bb50e501730543e188c6640daaa71b985ce9b977;hp=306071f96ccf23050e5a09789e699f9063342e65;hpb=097fd2a5e602116e9378a5ae5a7b5128ef004c2d;p=bcachefs-tools-debian diff --git a/crypto.c b/crypto.c index 306071f..32671bd 100644 --- a/crypto.c +++ b/crypto.c @@ -12,10 +12,10 @@ #include #include -#include +#include #include -#include "checksum.h" +#include "libbcachefs/checksum.h" #include "crypto.h" char *read_passphrase(const char *prompt) @@ -84,12 +84,13 @@ struct bch_key derive_passphrase(struct bch_sb_field_crypt *crypt, switch (BCH_CRYPT_KDF_TYPE(crypt)) { case BCH_KDF_SCRYPT: - ret = libscrypt_scrypt((void *) passphrase, strlen(passphrase), - salt, sizeof(salt), - 1ULL << BCH_KDF_SCRYPT_N(crypt), - 1ULL << BCH_KDF_SCRYPT_R(crypt), - 1ULL << BCH_KDF_SCRYPT_P(crypt), - (void *) &key, sizeof(key)); + ret = crypto_pwhash_scryptsalsa208sha256_ll( + (void *) passphrase, strlen(passphrase), + salt, sizeof(salt), + 1ULL << BCH_KDF_SCRYPT_N(crypt), + 1ULL << BCH_KDF_SCRYPT_R(crypt), + 1ULL << BCH_KDF_SCRYPT_P(crypt), + (void *) &key, sizeof(key)); if (ret) die("scrypt error: %i", ret); break; @@ -100,11 +101,19 @@ struct bch_key derive_passphrase(struct bch_sb_field_crypt *crypt, return key; } +bool bch2_sb_is_encrypted(struct bch_sb *sb) +{ + struct bch_sb_field_crypt *crypt; + + return (crypt = bch2_sb_field_get(sb, crypt)) && + bch2_key_is_encrypted(&crypt->key); +} + void bch2_passphrase_check(struct bch_sb *sb, const char *passphrase, struct bch_key *passphrase_key, struct bch_encrypted_key *sb_key) { - struct bch_sb_field_crypt *crypt = bch2_sb_get_crypt(sb); + struct bch_sb_field_crypt *crypt = bch2_sb_field_get(sb, crypt); if (!crypt) die("filesystem is not encrypted"); @@ -124,10 +133,23 @@ void bch2_passphrase_check(struct bch_sb *sb, const char *passphrase, die("incorrect passphrase"); } -void bch2_add_key(struct bch_sb *sb, const char *passphrase) +void bch2_add_key(struct bch_sb *sb, + const char *type, + const char *keyring_str, + const char *passphrase) { struct bch_key passphrase_key; struct bch_encrypted_key sb_key; + int keyring; + + if (!strcmp(keyring_str, "session")) + keyring = KEY_SPEC_SESSION_KEYRING; + else if (!strcmp(keyring_str, "user")) + keyring = KEY_SPEC_USER_KEYRING; + else if (!strcmp(keyring_str, "user_session")) + keyring = KEY_SPEC_USER_SESSION_KEYRING; + else + die("unknown keyring %s", keyring_str); bch2_passphrase_check(sb, passphrase, &passphrase_key, @@ -138,12 +160,10 @@ void bch2_add_key(struct bch_sb *sb, const char *passphrase) char *description = mprintf("bcachefs:%s", uuid); - if (add_key("logon", description, - &passphrase_key, sizeof(passphrase_key), - KEY_SPEC_USER_KEYRING) < 0 || - add_key("user", description, + if (add_key(type, + description, &passphrase_key, sizeof(passphrase_key), - KEY_SPEC_USER_KEYRING) < 0) + keyring) < 0) die("add_key error: %m"); memzero_explicit(description, strlen(description)); @@ -162,9 +182,9 @@ void bch_sb_crypt_init(struct bch_sb *sb, if (passphrase) { SET_BCH_CRYPT_KDF_TYPE(crypt, BCH_KDF_SCRYPT); - SET_BCH_KDF_SCRYPT_N(crypt, ilog2(SCRYPT_N)); - SET_BCH_KDF_SCRYPT_R(crypt, ilog2(SCRYPT_r)); - SET_BCH_KDF_SCRYPT_P(crypt, ilog2(SCRYPT_p)); + 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);