Skip to contentSkip to conversation

support OpenSSL's libcrypto and make libgcrypt optional

PATCH In review [email protected] 4 participants 5 messages Jul 28, 2010 – Jul 30, 2010 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 support OpenSSL's libcrypto and make libgcrypt optional

Hi,

In some embedded environments, like in some where MeeGo is used, it is needed to reduce number of packages with duplicate functionality in order to save flash space. Thus we have created a patch to add optional support for OpenSSL's libcrypto as well as make libgcrypt optional, making it possible to build cryptsetup in environments lacking either one. Or to support both, if that makes any sense.

Preferably, the hash plugin interface should be extended and used also in luks/ part, but we didn't start making such a drastic change.

The new OpenSSL plugin can be made cleaner once OpenSSL 1.x becomes more common, but at the moment the 0.x series is still widely used and lacks some of the APIs 1.x has.

Comments are more than welcome. :)

Best regards,

- Jussi Laako

configure.in +51 -2
index 936d165..3d45361 100644
@@ -31,6 +31,8 @@ AC_HEADER_STDC
 AC_CHECK_HEADERS(fcntl.h malloc.h inttypes.h sys/ioctl.h sys/mman.h \
 	ctype.h unistd.h locale.h)
 
+PKG_PROG_PKG_CONFIG([0.22])
+
 AC_CHECK_HEADERS(uuid/uuid.h,,[AC_MSG_ERROR('You need the uuid library')])
 AC_CHECK_HEADER(libdevmapper.h,,[AC_MSG_ERROR('You need the device-mapper library')])
 
@@ -40,8 +42,6 @@ AC_SUBST(UUID_LIBS, $LIBS)
 LIBS=$saved_LIBS
 
 
-AM_PATH_LIBGCRYPT(1.1.42,,[AC_MSG_ERROR('You need the gcrypt library')])
-
 AC_CHECK_FUNCS([posix_memalign])
 
 AC_C_CONST
@@ -73,6 +73,51 @@ AC_ARG_ENABLE(shared-library,
 enable_shared_library=yes)
 AM_CONDITIONAL(STATIC_LIBRARY, test x$enable_shared_library = xno)
 
+AC_ARG_ENABLE(plugins,
+[  --enable-plugins        disable shared library plugins],,enable_plugins=no)
+if test "x$enable_plugins" = xyes; then
+	AC_DEFINE(USE_PLUGINS, 1, [Define if you wish to use the plugin loader])
+fi
+
+AC_ARG_ENABLE(, [
+Modules:
+  --with-MODULE[[=shared]]  builds the module MODULE as a shared library plugin
+],,)
+
+AC_ARG_ENABLE(all,
+[  --disable-all           disable all modules built by default],,enable_all=default)
+
+AC_ARG_ENABLE(gcrypt,
+[  --disable-gcrypt        disable gcrypt support [[default=no]]], ,
+	enable_gcrypt=yes)
+if test x$enable_gcrypt = xyes; then
+	AC_CHECK_LIB(gcrypt, gcry_check_version, have_gcrypt=yes, have_gcrypt=no)
+fi
+AM_CONDITIONAL(BUILD_LIBGCRYPT, test x$have_gcrypt = xyes)
+AM_CONDITIONAL(SHARED_LIBGCRYPT,
+	test x$enable_shared_library = xyes -a x$have_gcrypt = xyes)
+if test x$have_gcrypt = xyes; then
+	LIBGCRYPT_CFLAGS="`libgcrypt-config --cflags`"
+	LIBGCRYPT_LIBS="`libgcrypt-config --libs`"
+	AC_SUBST(LIBGCRYPT_CFLAGS)
+	AC_SUBST(LIBGCRYPT_LIBS)
+fi
+
+AC_ARG_ENABLE(openssl,
+[  --disable-openssl       disable openssl support [[default=no]]], ,
+	enable_openssl=yes)
+if test x$enable_openssl = xyes; then
+	PKG_CHECK_MODULES([OPENSSL], [libcrypto >= 0.9.8], AC_CHECK_LIB(crypto, SHA512, have_openssl=yes, have_openssl=no),
+							    have_openssl=no)
+fi
+AM_CONDITIONAL(BUILD_OPENSSL, test x$have_openssl = xyes)
+AM_CONDITIONAL(SHARED_OPENSSL,
+	test x$enable_shared_library = xyes -a x$have_openssl = xyes)
+
+if test x$have_gcrypt != xyes -a x$have_openssl != xyes; then
+	AC_MSG_ERROR('You need openSSL or/and gcrypt library as a crypto backend')
+fi
+
 AC_ARG_ENABLE(selinux,
 [  --disable-selinux       disable selinux support [[default=auto]]],[], [])
 
@@ -87,6 +132,7 @@ if AC_RUN_LOG([pkg-config --exists --print-errors devmapper]); then
 	fi
 	PKG_CHECK_MODULES([DEVMAPPER], [devmapper >= 1.02.03])
 	LIBS="$LIBS $DEVMAPPER_LIBS"
+	AC_SUBST(LIBDEVMAPPER_CFLAGS)
 else
 	AC_CHECK_LIB(devmapper, dm_task_set_name,,
 		[AC_MSG_ERROR('You need the device-mapper library')])
@@ -95,6 +141,9 @@ else
 	DEVMAPPER_LIBS=$LIBS
 fi
 
+AM_CONDITIONAL(BUILD_LIBDEVMAPPER, test x$build_static = xyes)
+AM_CONDITIONAL(SHARED_LIBDEVMAPPER, test x$build_shared = xyes)
+
 AC_CHECK_DECLS([DM_UDEV_DISABLE_DISK_RULES_FLAG], [have_cookie=yes], [have_cookie=no], [#include <libdevmapper.h>])
 if test "x$enable_udev" = xyes; then
 	if test "x$have_cookie" = xno; then
lib/Makefile.am +43 -3

75 lines not on this page · raw message

lib/backends.c +43 -0

61 lines not on this page · raw message

lib/gcrypt.c +1 -1
index cfbcdc7..e68fd8b 100644
@@ -10,7 +10,7 @@
 #define MAX_DIGESTS		64
 #define GCRYPT_REQ_VERSION	"1.1.42"
 
-int init_crypto(void)
+int init_gcrypt(void)
 {
 	if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
 		if (!gcry_check_version (GCRYPT_REQ_VERSION))
lib/internal.h +13 -0
index 7b31cf8..6d741a7 100644
@@ -40,6 +40,19 @@ struct hash_backend {
 	void			(*free_hashes)(struct hash_type *hashes);
 };
 
+struct setup_backend {
+	const char	*name;
+	int		(*init)(void);
+	void		(*exit)(void);
+	int		(*create)(int reload, struct crypt_options *options,
+			          const char *key, const char *uuid);
+	int		(*status)(int details, struct crypt_options *options,
+			          char **key);
+	int		(*remove)(int force, struct crypt_options *options);
+
+	const char *	(*dir)(void);
+};
+
 struct device_infos {
 	uint64_t	size;
 	int		readonly;
lib/openssl.c new file +306 -0

306 lines not on this page · raw message

lib/setup.c +21 -0
index 720ea9b..0fcf912 100644
@@ -38,6 +38,27 @@ struct crypt_device {
 	void *password_usrptr;
 };
 
+#ifdef BUILTIN_OPENSSL
+int init_openssl(void);
+#endif
+#ifdef BUILTIN_GCRYPT
+int init_gcrypt(void);
+#endif
+
+int init_crypto(void)
+{
+	int i = 0;
+#ifdef BUILTIN_OPENSSL
+	if ((i = init_openssl()) != 0)
+		return i;
+#endif
+#ifdef BUILTIN_GCRYPT
+	if ((i = init_gcrypt()) != 0)
+		return i;
+#endif
+	return i;
+}
+
 /* Log helper */
 static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
 static int _debug_level = 0;
luks/Makefile.am +12 -1
index 7ce43ca..6c96083 100644
@@ -2,7 +2,16 @@ moduledir = $(libdir)/cryptsetup
 
 noinst_LTLIBRARIES = libluks.la
 
-libluks_la_CFLAGS = -Wall @LIBGCRYPT_CFLAGS@
+libluks_la_CFLAGS = -Wall
+
+if BUILD_LIBGCRYPT
+libluks_la_CFLAGS += @LIBGCRYPT_CFLAGS@
+_LIBGCRYPT_CFLAGS = -DBUILTIN_GCRYPT
+endif
+
+if BUILD_OPENSSL
+_OPENSSL_CFLAGS = -DBUILTIN_OPENSSL
+endif
 
 libluks_la_SOURCES = \
 	af.c \
@@ -19,5 +28,7 @@ libluks_la_SOURCES = \
 INCLUDES = -D_GNU_SOURCE			\
 	-D_LARGEFILE64_SOURCE                   \
 	-D_FILE_OFFSET_BITS=64                  \
+	$(_OPENSSL_CFLAGS)			\
+	$(_LIBGCRYPT_CFLAGS)			\
         -I$(top_srcdir)/lib
 
luks/af.c +47 -1

95 lines not on this page · raw message

luks/pbkdf.c +64 -4

141 lines not on this page · raw message

tests/align-test +1 -0
index 2fdb9b6..780d8f8 100755
@@ -98,5 +98,6 @@ dmsetup create luks0xbabe --table "0 32768 linear $DEV2 0"
 format 256 2112
 format 128 1088
 format 128 8192 8192
+sync
 dmsetup remove luks0xbabe
 cleanup
tests/compat-test +1 -0
index b7f6589..b4c8fac 100755
@@ -41,6 +41,7 @@ function prepare()
 		exit 0
 	fi
 
+	sync
 	[ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
 
 	if [ ! -e $KEY1 ]; then
#1 lore raw
Quoted 6 lines · On 07/28/2010 10:33 AM, [email protected] wrote:

In some embedded environments, like in some where MeeGo is used, it is needed to reduce number of packages with duplicate functionality in order to save flash space. Thus we have created a patch to add optional support for OpenSSL's libcrypto as well as make libgcrypt optional, making it possible to build cryptsetup in environments lacking either one. Or to support both, if that makes any sense.

Hi,

I intentionally removed plugin interface and [lib]cryptsetup now depends on libgcrypt.

Well, since that decision I found some problems in gcrypt (which upstream refused to fix) so seems there is now yet another reason to support more crypto backends. (It should support gcrypt, nss, openssl at least).

But I do not want to use runtime plugin architecture but compile time decision only.

So for you patch:
- seems you are reverting some configure options which are not needed, I think it is enough to have someting like --with-crypto=[gcrypt,openssl,nss] or so.

- setup_backend is not needed, it will always depend on device-mapper (did I miss anything? it is not used even in your patch...just defined there)

- using #ifdef is not ideal, mainly in crypto code - it is easy to break algorithm when using wrong defines. It need properly define crypto backend callbacks and switch only its implementations. And include tests (I have already PBKDF2 testvectors test, that should be used for all backends.)

- I think openssl backend function should not duplicate all the code for every algorithm

- you added some "sync" calls to test - is it another problem? (should not be needed at all)

- there is still hardcoded gcrypt logic on some places (including api-test), so it still links to gcrypt, all regression tests must run with all supported backends. Probably some basic list of must-have algorithms should be defined for crypto backends. (e.g. I am using whirlpool hash for testing, not all crypto backend support it currently.)

So, in general, I agree we should support more crypto backends.

Are you ok with supporting this in compile time only (so there is always only one backend in compiled binaries - depends on distro preference)?

If so, I'll add this to my TODO list for next versions.

Thanks,
Milan

#2 lore raw
Quoted 8 lines · Milan Broz
Quoted 6 lines · On 07/28/2010 10:33 AM, [email protected] wrote:

In some embedded environments, like in some where MeeGo is used, it is needed to reduce number of packages with duplicate functionality in order to save flash space. Thus we have created a patch to add optional support for OpenSSL's libcrypto as well as make libgcrypt optional, making it possible to build cryptsetup in environments lacking either one. Or to support both, if that makes any sense.

Quoted 8 lines

Hi,

I intentionally removed plugin interface and [lib]cryptsetup now depends on libgcrypt.

Well, since that decision I found some problems in gcrypt (which upstream refused to fix) so seems there is now yet another reason to support more crypto backends. (It should support gcrypt, nss, openssl at least).

Well, noting is ever perfect, so avoiding strong dependencies is definitely a good idea.

Quoted 2 lines

But I do not want to use runtime plugin architecture but compile time decision only.

I like that for security resons, due to increased clarity about what is used and lower complexity.

Quoted 4 lines

So for you patch:
- seems you are reverting some configure options which are not needed, I think it is enough to have someting like --with-crypto=[gcrypt,openssl,nss] or so.

I agree.

Quoted 8 lines

- setup_backend is not needed, it will always depend on device-mapper (did I miss anything? it is not used even in your patch...just defined there)

- using #ifdef is not ideal, mainly in crypto code - it is easy to break algorithm when using wrong defines. It need properly define crypto backend callbacks and switch only its implementations. And include tests (I have already PBKDF2 testvectors test, that should be used for all backends.)

Uniform tests are definitely a very good idea.

Quoted 11 lines

- I think openssl backend function should not duplicate all the code for every algorithm

- you added some "sync" calls to test - is it another problem? (should not be needed at all)

- there is still hardcoded gcrypt logic on some places (including api-test), so it still links to gcrypt, all regression tests must run with all supported backends. Probably some basic list of must-have algorithms should be defined for crypto backends. (e.g. I am using whirlpool hash for testing, not all crypto backend support it currently.)

I agree. Which algorithms are supported by all backend and which require a specific backend will also become a FAQ item ;-)

We could run a poll here for a month or so about what people consider must-have.

Quoted 7 lines

So, in general, I agree we should support more crypto backends.

Are you ok with supporting this in compile time only (so there is always only one backend in compiled binaries - depends on distro preference)?

If so, I'll add this to my TODO list for next versions.

I would support that. Seems like a good idea.

Arno

Signature · 7 lines
Arno Wagner, Dr. sc. techn., Dipl. Inform., CISSP -- Email: [email protected] 
GnuPG:  ID: 1E25338F  FP: 0C30 5782 9D93 F785 E79C  0296 797F 6B50 1E25 338F
----
Cuddly UI's are the manifestation of wishful thinking. -- Dylan Evans

If it's in the news, don't worry about it.  The very definition of 
"news" is "something that hardly ever happens." -- Bruce Schneier 
#3 lore raw
In reply to Milan Broz

Hi,

Quoted 2 lines

- you added some "sync" calls to test - is it another problem? (should not be needed at all)

I added 'sync' because otherwise sometimes in tests this command:

  # dmsetup remove volumeName

failed to remove volume and I got this error:

device-mapper: remove ioctl failed: Device or resource busy

Initially I added 'sleep 1' before 'dmsetup', but then decided to replace it with 'sync'. 'sync' also did the trick. So I kept it.
I didn't try to figure out what's the problem with 'dmsetup remove'

Best Regards,
Alexander Valitov
________________________________________
From: Milan Broz [[email protected]]
Sent: 29 July 2010 15:01
To: [email protected]
Cc: [email protected]; [email protected]; Valitov Alexander
Subject: Re: [PATCH] support OpenSSL's libcrypto and make libgcrypt optional

Quoted 6 lines · On 07/28/2010 10:33 AM, [email protected] wrote:

In some embedded environments, like in some where MeeGo is used, it is needed to reduce number of packages with duplicate functionality in order to save flash space. Thus we have created a patch to add optional support for OpenSSL's libcrypto as well as make libgcrypt optional, making it possible to build cryptsetup in environments lacking either one. Or to support both, if that makes any sense.

Hi,

I intentionally removed plugin interface and [lib]cryptsetup now depends on libgcrypt.

Well, since that decision I found some problems in gcrypt (which upstream refused to fix) so seems there is now yet another reason to support more crypto backends. (It should support gcrypt, nss, openssl at least).

But I do not want to use runtime plugin architecture but compile time decision only.

So for you patch:
- seems you are reverting some configure options which are not needed, I think it is enough to have someting like --with-crypto=[gcrypt,openssl,nss] or so.

- setup_backend is not needed, it will always depend on device-mapper (did I miss anything? it is not used even in your patch...just defined there)

- using #ifdef is not ideal, mainly in crypto code - it is easy to break algorithm when using wrong defines. It need properly define crypto backend callbacks and switch only its implementations. And include tests (I have already PBKDF2 testvectors test, that should be used for all backends.)

- I think openssl backend function should not duplicate all the code for every algorithm

- you added some "sync" calls to test - is it another problem? (should not be needed at all)

- there is still hardcoded gcrypt logic on some places (including api-test), so it still links to gcrypt, all regression tests must run with all supported backends. Probably some basic list of must-have algorithms should be defined for crypto backends. (e.g. I am using whirlpool hash for testing, not all crypto backend support it currently.)

So, in general, I agree we should support more crypto backends.

Are you ok with supporting this in compile time only (so there is always only one backend in compiled binaries - depends on distro preference)?

If so, I'll add this to my TODO list for next versions.

Thanks,
Milan

#4 lore raw
In reply to Milan Broz

Hi,

Quoted 2 lines · ext Milan Broz

But I do not want to use runtime plugin architecture but compile time decision only.

Sounds reasonable.

Quoted 4 lines

So for you patch:
- seems you are reverting some configure options which are not needed, I think it is enough to have someting like --with-crypto=[gcrypt,openssl,nss] or so.

I can try to look into this later, though I'm not an autotool wizard... :)

Quoted 5 lines

- using #ifdef is not ideal, mainly in crypto code - it is easy to break algorithm when using wrong defines. It need properly define crypto backend callbacks and switch only its implementations. And include tests (I have already PBKDF2 testvectors test, that should be used for all backends.)

Sounds good, I can update the OpenSSL specific code once the needed backend changes are there.

Quoted 2 lines

- I think openssl backend function should not duplicate all the code for every algorithm

True, there's no really nice way to have it with this backend interface using the low-level OpenSSL crypto API. I rewrote the backend (attached) to use a bit higher level EVP interface which made it a lot cleaner. The initialization part is needed when the EVP_get_digestbyname() is in use. When not using the name lookups but the code below, it shouldn't need separate initialization.

Also needed once extra change to backends.c in order to call the initialization. (incremental patch attached)

Quoted 5 lines

- there is still hardcoded gcrypt logic on some places (including api-test), so it still links to gcrypt, all regression tests must run with all supported backends. Probably some basic list of must-have algorithms should be defined for crypto backends. (e.g. I am using whirlpool hash for testing, not all crypto backend support it currently.)

I'll look into this in September, unless Alexander has time to take a look at that part earlier.

Quite common older versions of OpenSSL don't have whirlpool, but 1.x series has it.

Quoted 2 lines

Are you ok with supporting this in compile time only (so there is always only one backend in compiled binaries - depends on distro preference)?

Yes, that's OK, since we just like to avoid forcefully bringing in duplicate functionality through dependencies, when possible.

Best regards,

- Jussi

lib/backends.c +3 -4
index 27e20a1..c451ac3 100644
@@ -10,13 +10,12 @@ extern struct hash_backend hash_gcrypt_backend;
 extern struct hash_backend hash_openssl_backend;
 extern struct setup_backend setup_libdevmapper_backend;
 
-#ifdef USE_PLUGINS
 static void init_plugins(void)
 {
+#ifdef BUILTIN_OPENSSL
+	init_openssl();
+#endif /* BUILTIN_OPENSSL */
 }
-#else /* USE_PLUGINS */
-#	define init_plugins()	do { } while(0)
-#endif /* USE_PLUGINS */
 
 static struct hash_backend *hash_backends[] = {
 #ifdef BUILTIN_GCRYPT