Skip to contentSkip to conversation

Use scrypt from libsodium

PATCH Superseded Chris Webb 1 participant 1 message Oct 7, 2021 Kbuild

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chris Webb

author
#0 lore raw

PATCH 1/1 Use scrypt from libsodium

bcachefs-tools has both libscrypt and libsodium as build dependencies, but libsodium already includes the same scrypt implementation as libscrypt, originally written by Colin Percival.

Use the libsodium copy, dropping the extra libscrypt dependency.

Explicitly adopt the default scrypt N, r and p values from libscrypt to avoid unintended changes in the default work parameters for bcachefs.

---
 .travis.yml                   |  1 -
 INSTALL                       |  7 +++----
 Makefile                      |  2 +-
 crypto.c                      | 21 +++++++++++----------
 debian/control                |  2 +-
 default.nix                   |  2 +-
 packaging/bcachefs-tools.spec |  2 --
 7 files changed, 17 insertions(+), 20 deletions(-)
.travis.yml +0 -1
index 3b90b73..399d5bc 100644
@@ -18,7 +18,6 @@ addons:
             - libblkid-dev
             - libkeyutils-dev
             - liblz4-dev
-            - libscrypt-dev
             - libsodium-dev
             - liburcu-dev
             - libzstd-dev
INSTALL +3 -4
index 85c09a2..c9ff28a 100644
@@ -6,7 +6,6 @@ Dependencies:
  * libblkid
  * libkeyutils
  * liblz4
- * libscrypt
  * libsodium
  * liburcu
  * libuuid
@@ -17,17 +16,17 @@ Dependencies:
 
 Debian (Bullseye or later) and Ubuntu (20.04 or later): you can install these with
     apt install -y pkg-config libaio-dev libblkid-dev libkeyutils-dev \
-        liblz4-dev libscrypt-dev libsodium-dev liburcu-dev libzstd-dev \
+        liblz4-dev libsodium-dev liburcu-dev libzstd-dev \
         uuid-dev zlib1g-dev valgrind libudev-dev
 
 Fedora: install the "Development tools" group along with:
     dnf install -y libaio-devel libsodium-devel \
         libblkid-devel libzstd-devel zlib-devel userspace-rcu-devel \
         lz4-devel libuuid-devel valgrind-devel keyutils-libs-devel \
-        libscrypt-devel findutils
+        findutils
 
 Arch: install bcachefs-tools-git from the AUR.
-Or to build from source, install libscrypt from the AUR along with,
+Or to build from source, install build dependencies with
     pacman -S base-devel libaio keyutils libsodium liburcu zstd valgrind
 
 Then, just make && make install
Makefile +1 -1
index 23e0508..e9d701e 100644
@@ -50,7 +50,7 @@ endif
 CFLAGS+=$(PKGCONFIG_CFLAGS)
 LDLIBS+=$(PKGCONFIG_LDLIBS)
 
-LDLIBS+=-lm -lpthread -lrt -lscrypt -lkeyutils -laio -ldl
+LDLIBS+=-lm -lpthread -lrt -lkeyutils -laio -ldl
 LDLIBS+=$(EXTRA_LDLIBS)
 
 ifeq ($(PREFIX),/usr)
crypto.c +11 -10
index 7f7fbd5..43753a3 100644
@@ -12,7 +12,7 @@
 
 #include <keyutils.h>
 #include <linux/random.h>
-#include <libscrypt.h>
+#include <sodium/crypto_pwhash_scryptsalsa208sha256.h>
 #include <uuid/uuid.h>
 
 #include "libbcachefs/checksum.h"
@@ -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;
@@ -170,9 +171,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);
 
debian/control +1 -1
index 2120504..0f766c2 100644
@@ -4,7 +4,7 @@ Section: utils
 Priority: optional
 Standards-Version: 3.9.5
 Build-Depends: debhelper (>= 9), pkg-config, libaio-dev, libblkid-dev,
-	libkeyutils-dev, liblz4-dev, libscrypt-dev, libsodium-dev, liburcu-dev,
+	libkeyutils-dev, liblz4-dev, libsodium-dev, liburcu-dev,
 	libzstd-dev, uuid-dev, zlib1g-dev
 Homepage: https://bcachefs.org/
 
default.nix +1 -1
index f19ff10..a53667d 100644
@@ -12,7 +12,7 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = [ git pkgconfig ];
   buildInputs =
     [ liburcu libuuid libaio zlib attr keyutils
-      libsodium libscrypt
+      libsodium
     ];
 
   enableParallelBuilding = true;
packaging/bcachefs-tools.spec +0 -2
index 4946cef..00d0fbb 100644
@@ -15,7 +15,6 @@ BuildRequires:  keyutils-libs-devel
 BuildRequires:  libaio-devel
 BuildRequires:  libattr-devel
 BuildRequires:  libblkid-devel
-BuildRequires:  libscrypt-devel
 BuildRequires:  libsodium-devel
 BuildRequires:  libtool-ltdl-devel
 BuildRequires:  libuuid-devel
@@ -32,7 +31,6 @@ Requires:   keyutils-libs
 Requires:   libaio
 Requires:   libattr
 Requires:   libblkid
-Requires:   libscrypt
 Requires:   libsodium
 Requires:   libtool-ltdl
 Requires:   libuuid