Skip to contentSkip to conversation

Qcrypto next patches

GIT PULL Applied Daniel P. Berrangé 3 participants 9 messages Jun 15, 2020 – Jun 16, 2020 Crypto API

lore
Share & tools
lore
#0 lore raw

The following changes since commit 7d3660e79830a069f1848bb4fa1cdf8f666424fb:

Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into stagi=
ng (2020-06-12 23:06:22 +0100)

are available in the Git repository at:

https://github.com/berrange/qemu tags/qcrypto-next-pull-request

for you to fetch changes up to d6cca8e111696fbbd7c233dc53f9c80b6a43359d:

crypto: Remove use of GCRYPT_VERSION macro. (2020-06-15 11:33:51 +0100)

----------------------------------------------------------------
Misc crypto subsystem fixes

* Improve error message for large files when creating LUKS volumes
* Expand crypto hash benchmark coverage
* Misc code refactoring with no functional change

----------------------------------------------------------------

Alexey Krasikov (3):
crypto/secret: move main logic from 'secret' to 'secret_common'. crypto/linux_keyring: add 'secret_keyring' secret object.
test-crypto-secret: add 'secret_keyring' object tests.

Marek Marczykowski-G=C3=B3recki (1):
crypto: add "none" random provider

Richard W.M. Jones (1):
crypto: Remove use of GCRYPT_VERSION macro.

 configure                       |  80 +++++++
 crypto/Makefile.objs            |   5 +-
 crypto/init.c                   |   2 +-
 crypto/random-none.c            |  38 +++
 crypto/secret.c                 | 347 +--------------------------
 crypto/secret_common.c          | 403 ++++++++++++++++++++++++++++++++
 crypto/secret_keyring.c         | 148 ++++++++++++
 include/crypto/secret.h         |  20 +-
 include/crypto/secret_common.h  |  68 ++++++
 include/crypto/secret_keyring.h |  52 +++++
 tests/Makefile.include          |   4 +
 tests/test-crypto-secret.c      | 158 +++++++++++++
 12 files changed, 966 insertions(+), 359 deletions(-)
 create mode 100644 crypto/random-none.c
 create mode 100644 crypto/secret_common.c
 create mode 100644 crypto/secret_keyring.c
 create mode 100644 include/crypto/secret_common.h
 create mode 100644 include/crypto/secret_keyring.h

--=20
2.26.2

#1 lore raw

[PULL 1/5] crypto: add "none" random provider

From: Marek Marczykowski-Górecki <[email protected]>

In case of not using random-number needing feature, it makes sense to skip RNG init too. This is especially helpful when QEMU is sandboxed in Stubdomain under Xen, where there is very little entropy so initial getrandom() call delays the startup several seconds. In that setup, no random bytes are needed at all.

---
 configure            | 11 +++++++++++
 crypto/Makefile.objs |  3 ++-
 crypto/random-none.c | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 crypto/random-none.c
configure +11 -0
index bb7fd12612..997284e094 100755
@@ -509,6 +509,7 @@ libpmem=""
 default_devices="yes"
 plugins="no"
 fuzzing="no"
+rng_none="no"
 
 supported_cpu="no"
 supported_os="no"
@@ -1601,6 +1602,10 @@ for opt do
   ;;
   --gdb=*) gdb_bin="$optarg"
   ;;
+  --enable-rng-none) rng_none=yes
+  ;;
+  --disable-rng-none) rng_none=no
+  ;;
   *)
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
@@ -1898,6 +1903,7 @@ disabled with --disable-FEATURE, default is enabled if available:
   debug-mutex     mutex debugging support
   libpmem         libpmem support
   xkbcommon       xkbcommon support
+  rng-none        dummy RNG, avoid using /dev/(u)random and getrandom()
 
 NOTE: The object files are built at the place where configure is launched
 EOF
@@ -6767,6 +6773,7 @@ echo "default devices   $default_devices"
 echo "plugin support    $plugins"
 echo "fuzzing support   $fuzzing"
 echo "gdb               $gdb_bin"
+echo "rng-none          $rng_none"
 
 if test "$supported_cpu" = "no"; then
     echo
@@ -7744,6 +7751,10 @@ if test "$edk2_blobs" = "yes" ; then
   echo "DECOMPRESS_EDK2_BLOBS=y" >> $config_host_mak
 fi
 
+if test "$rng_none" = "yes"; then
+  echo "CONFIG_RNG_NONE=y" >> $config_host_mak
+fi
+
 # use included Linux headers
 if test "$linux" = "yes" ; then
   mkdir -p linux-headers
crypto/Makefile.objs +2 -1
index c2a371b0b4..cdee92b4e5 100644
@@ -35,5 +35,6 @@ crypto-obj-y += block-luks.o
 
 util-obj-$(CONFIG_GCRYPT) += random-gcrypt.o
 util-obj-$(if $(CONFIG_GCRYPT),n,$(CONFIG_GNUTLS)) += random-gnutls.o
-util-obj-$(if $(CONFIG_GCRYPT),n,$(if $(CONFIG_GNUTLS),n,y)) += random-platform.o
+util-obj-$(if $(CONFIG_GCRYPT),n,$(if $(CONFIG_GNUTLS),n,$(CONFIG_RNG_NONE))) += random-none.o
+util-obj-$(if $(CONFIG_GCRYPT),n,$(if $(CONFIG_GNUTLS),n,$(if $(CONFIG_RNG_NONE),n,y))) += random-platform.o
 util-obj-y += aes.o init.o
crypto/random-none.c new file +38 -0
new file mode 100644
index 0000000000..102f8a4dce
@@ -0,0 +1,38 @@
+/*
+ * QEMU Crypto "none" random number provider
+ *
+ * Copyright (c) 2020 Marek Marczykowski-Górecki
+ *                      <[email protected]>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include "qemu/osdep.h"
+
+#include "crypto/random.h"
+#include "qapi/error.h"
+
+int qcrypto_random_init(Error **errp)
+{
+    return 0;
+}
+
+int qcrypto_random_bytes(void *buf,
+                         size_t buflen,
+                         Error **errp)
+{
+    error_setg(errp, "Random bytes not available with \"none\" rng");
+    return -1;
+}
Signature · 1 line
2.26.2
#2 lore raw

[PULL 5/5] crypto: Remove use of GCRYPT_VERSION macro.

In reply to Daniel P. Berrangé

From: "Richard W.M. Jones" <[email protected]>

According to the gcrypt documentation it's intended that gcry_check_version() is called with the minimum version of gcrypt needed by the program, not the version from the <gcrypt.h> header file that happened to be installed when qemu was compiled. Indeed the gcrypt.h header says that you shouldn't use the GCRYPT_VERSION macro.

This causes the following failure:

qemu-img: Unable to initialize gcrypt

if a slightly older version of libgcrypt is installed with a newer qemu, even though the slightly older version works fine. This can happen with RPM packaging which uses symbol versioning to determine automatically which libgcrypt is required by qemu, which caused the following bug in RHEL 8:

https://bugzilla.redhat.com/show_bug.cgi?id=1840485

qemu actually requires libgcrypt >= 1.5.0, so we might put the string "1.5.0" here. However since 1.5.0 was released in 2011, it hardly seems we need to check that. So I replaced GCRYPT_VERSION with NULL. Perhaps in future if we move to requiring a newer version of gcrypt we could put a literal string here.

---
 crypto/init.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
crypto/init.c +1 -1
index b305381ec5..ea233b9192 100644
@@ -122,7 +122,7 @@ int qcrypto_init(Error **errp)
 #endif
 
 #ifdef CONFIG_GCRYPT
-    if (!gcry_check_version(GCRYPT_VERSION)) {
+    if (!gcry_check_version(NULL)) {
         error_setg(errp, "Unable to initialize gcrypt");
         return -1;
     }
Signature · 1 line
2.26.2
#3 lore raw

[PULL 4/5] test-crypto-secret: add 'secret_keyring' object tests.

In reply to Daniel P. Berrangé

From: Alexey Krasikov <[email protected]>

Add tests:
test_secret_keyring_good;
test_secret_keyring_revoked_key;
test_secret_keyring_expired_key;
test_secret_keyring_bad_serial_key;
test_secret_keyring_bad_key_access_right;

Added tests require libkeyutils. The absence of this library is not critical, because these tests will be skipped in this case.

---
 configure                  |  24 ++++++
 tests/Makefile.include     |   4 +
 tests/test-crypto-secret.c | 158 +++++++++++++++++++++++++++++++++++++
 3 files changed, 186 insertions(+)
configure +24 -0
index 3fbb61905a..07202acb9e 100755
@@ -6330,6 +6330,27 @@ but not implemented on your system"
     fi
 fi
 
+##########################################
+# check for usable keyutils.h
+
+if test "$linux" = "yes" ; then
+
+    have_keyutils=no
+    cat > $TMPC << EOF
+#include <errno.h>
+#include <asm/unistd.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <keyutils.h>
+int main(void) {
+    return request_key("user", NULL, NULL, 0);
+}
+EOF
+    if compile_prog "" "-lkeyutils"; then
+        have_keyutils=yes
+    fi
+fi
+
 
 ##########################################
 # End of CC checks
@@ -7702,6 +7723,9 @@ fi
 
 if test "$secret_keyring" = "yes" ; then
   echo "CONFIG_SECRET_KEYRING=y" >> $config_host_mak
+  if test "$have_keyutils" = "yes" ; then
+    echo "CONFIG_TEST_SECRET_KEYRING=y" >> $config_host_mak
+  fi
 fi
 
 if test "$tcg_interpreter" = "yes"; then
tests/Makefile.include +4 -0
index c2397de8ed..5607c7290d 100644
@@ -540,6 +540,10 @@ tests/benchmark-crypto-cipher$(EXESUF): tests/benchmark-crypto-cipher.o $(test-c
 tests/test-crypto-secret$(EXESUF): tests/test-crypto-secret.o $(test-crypto-obj-y)
 tests/test-crypto-xts$(EXESUF): tests/test-crypto-xts.o $(test-crypto-obj-y)
 
+ifeq ($(CONFIG_TEST_SECRET_KEYRING),y)
+tests/test-crypto-secret.o-libs := -lkeyutils
+endif
+
 tests/crypto-tls-x509-helpers.o-cflags := $(TASN1_CFLAGS)
 tests/crypto-tls-x509-helpers.o-libs := $(TASN1_LIBS)
 tests/pkix_asn1_tab.o-cflags := $(TASN1_CFLAGS)
tests/test-crypto-secret.c +158 -0

176 lines not on this page · raw message

Signature · 1 line
2.26.2
#4 lore raw

[PULL 2/5] crypto/secret: move main logic from 'secret' to 'secret_common'.

In reply to Daniel P. Berrangé

From: Alexey Krasikov <[email protected]>

Create base class 'common secret'. Move common data and logic from 'secret' to 'common_secret' class. This allowed adding abstraction layer for easier adding new 'secret' objects in future.
Convert 'secret' class to child from basic 'secret_common' with 'data' and 'file' properties.

---
 crypto/Makefile.objs           |   1 +
 crypto/secret.c                | 347 +---------------------------
 crypto/secret_common.c         | 403 +++++++++++++++++++++++++++++++++
 include/crypto/secret.h        |  20 +-
 include/crypto/secret_common.h |  68 ++++++
 5 files changed, 482 insertions(+), 357 deletions(-)
 create mode 100644 crypto/secret_common.c
 create mode 100644 include/crypto/secret_common.h
crypto/Makefile.objs +1 -0
index cdee92b4e5..110dec1b87 100644
@@ -18,6 +18,7 @@ crypto-obj-y += tlscredsanon.o
 crypto-obj-y += tlscredspsk.o
 crypto-obj-y += tlscredsx509.o
 crypto-obj-y += tlssession.o
+crypto-obj-y += secret_common.o
 crypto-obj-y += secret.o
 crypto-obj-y += pbkdf.o
 crypto-obj-$(CONFIG_NETTLE) += pbkdf-nettle.o
crypto/secret.c +7 -340

404 lines not on this page · raw message

crypto/secret_common.c new file +403 -0

403 lines not on this page · raw message

include/crypto/secret.h +3 -17
index 5e07e29bae..2deb461d2f 100644
@@ -23,6 +23,7 @@
 
 #include "qapi/qapi-types-crypto.h"
 #include "qom/object.h"
+#include "crypto/secret_common.h"
 
 #define TYPE_QCRYPTO_SECRET "secret"
 #define QCRYPTO_SECRET(obj)                  \
@@ -119,29 +120,14 @@ typedef struct QCryptoSecretClass QCryptoSecretClass;
  */
 
 struct QCryptoSecret {
-    Object parent_obj;
-    uint8_t *rawdata;
-    size_t rawlen;
-    QCryptoSecretFormat format;
+    QCryptoSecretCommon parent_obj;
     char *data;
     char *file;
-    char *keyid;
-    char *iv;
 };
 
 
 struct QCryptoSecretClass {
-    ObjectClass parent_class;
+    QCryptoSecretCommonClass parent_class;
 };
 
-
-extern int qcrypto_secret_lookup(const char *secretid,
-                                 uint8_t **data,
-                                 size_t *datalen,
-                                 Error **errp);
-extern char *qcrypto_secret_lookup_as_utf8(const char *secretid,
-                                           Error **errp);
-extern char *qcrypto_secret_lookup_as_base64(const char *secretid,
-                                             Error **errp);
-
 #endif /* QCRYPTO_SECRET_H */
include/crypto/secret_common.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
2.26.2
#5 lore raw

[PULL 3/5] crypto/linux_keyring: add 'secret_keyring' secret object.

In reply to Daniel P. Berrangé

From: Alexey Krasikov <[email protected]>

Add the ability for the secret object to obtain secret data from the Linux in-kernel key managment and retention facility, as an extra option to the existing ones: reading from a file or passing directly as a string.

The secret is identified by the key serial number. The upper layers need to instantiate the key and make sure the QEMU process has access permissions to read it.

- Fixed up detection logic default behaviour in configure

---
 configure                       |  45 ++++++++++
 crypto/Makefile.objs            |   1 +
 crypto/secret_keyring.c         | 148 ++++++++++++++++++++++++++++++++
 include/crypto/secret_keyring.h |  52 +++++++++++
 4 files changed, 246 insertions(+)
 create mode 100644 crypto/secret_keyring.c
 create mode 100644 include/crypto/secret_keyring.h
configure +45 -0
index 997284e094..3fbb61905a 100755
@@ -510,6 +510,7 @@ default_devices="yes"
 plugins="no"
 fuzzing="no"
 rng_none="no"
+secret_keyring=""
 
 supported_cpu="no"
 supported_os="no"
@@ -1606,6 +1607,10 @@ for opt do
   ;;
   --disable-rng-none) rng_none=no
   ;;
+  --enable-keyring) secret_keyring="yes"
+  ;;
+  --disable-keyring) secret_keyring="no"
+  ;;
   *)
       echo "ERROR: unknown option $opt"
       echo "Try '$0 --help' for more information"
@@ -6290,6 +6295,41 @@ case "$slirp" in
     ;;
 esac
 
+##########################################
+# check for usable __NR_keyctl syscall
+
+if test "$linux" = "yes" ; then
+
+    have_keyring=no
+    cat > $TMPC << EOF
+#include <errno.h>
+#include <asm/unistd.h>
+#include <linux/keyctl.h>
+#include <unistd.h>
+int main(void) {
+    return syscall(__NR_keyctl, KEYCTL_READ, 0, NULL, NULL, 0);
+}
+EOF
+    if compile_prog "" "" ; then
+        have_keyring=yes
+    fi
+fi
+if test "$secret_keyring" != "no"
+then
+    if test "$have_keyring" == "yes"
+    then
+	secret_keyring=yes
+    else
+	if test "$secret_keyring" = "yes"
+	then
+	    error_exit "syscall __NR_keyctl requested, \
+but not implemented on your system"
+	else
+	    secret_keyring=no
+	fi
+    fi
+fi
+
 
 ##########################################
 # End of CC checks
@@ -6774,6 +6814,7 @@ echo "plugin support    $plugins"
 echo "fuzzing support   $fuzzing"
 echo "gdb               $gdb_bin"
 echo "rng-none          $rng_none"
+echo "Linux keyring     $secret_keyring"
 
 if test "$supported_cpu" = "no"; then
     echo
@@ -7659,6 +7700,10 @@ if test -n "$gdb_bin" ; then
     echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak
 fi
 
+if test "$secret_keyring" = "yes" ; then
+  echo "CONFIG_SECRET_KEYRING=y" >> $config_host_mak
+fi
+
 if test "$tcg_interpreter" = "yes"; then
   QEMU_INCLUDES="-iquote \$(SRC_PATH)/tcg/tci $QEMU_INCLUDES"
 elif test "$ARCH" = "sparc64" ; then
crypto/Makefile.objs +1 -0
index 110dec1b87..707c02ad37 100644
@@ -20,6 +20,7 @@ crypto-obj-y += tlscredsx509.o
 crypto-obj-y += tlssession.o
 crypto-obj-y += secret_common.o
 crypto-obj-y += secret.o
+crypto-obj-$(CONFIG_SECRET_KEYRING) += secret_keyring.o
 crypto-obj-y += pbkdf.o
 crypto-obj-$(CONFIG_NETTLE) += pbkdf-nettle.o
 crypto-obj-$(if $(CONFIG_NETTLE),n,$(CONFIG_GCRYPT)) += pbkdf-gcrypt.o
crypto/secret_keyring.c new file +148 -0

148 lines not on this page · raw message

include/crypto/secret_keyring.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
2.26.2
#6 lore raw

Re: [PULL 3/5] crypto/linux_keyring: add 'secret_keyring' secret object.

configure case "$slirp" in · line 6319

Quoted 3 lines · Daniel P. Berrangé
configure +3 -0
+if test "$secret_keyring" != "no"
+then
+    if test "$have_keyring" == "yes"

This generates a complaint when building on Debian testing:

./configure: 6319: test: yes: unexpected operator

Perhaps should be a single = ?

configure case "$slirp" in · line 6332

Quoted 13 lines
configure +13 -0
+    then
+	secret_keyring=yes
+    else
+	if test "$secret_keyring" = "yes"
+	then
+	    error_exit "syscall __NR_keyctl requested, \
+but not implemented on your system"
+	else
+	    secret_keyring=no
+	fi
+    fi
+fi
+

dme.

Signature · 1 line
The sound of a barking dog on a loop, a plane rises in the crystal blue.
#7 lore raw

Re: [PULL 3/5] crypto/linux_keyring: add 'secret_keyring' secret object.

Quoted 11 lines · David Edmondson

configure case "$slirp" in · line 6319

Quoted 3 lines · Daniel P. Berrangé
configure +3 -0
+if test "$secret_keyring" != "no"
+then
+    if test "$have_keyring" == "yes"

This generates a complaint when building on Debian testing:

./configure: 6319: test: yes: unexpected operator

Perhaps should be a single = ?

Yes, you are right.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#8 lore raw
In reply to Daniel P. Berrangé
Quoted 12 lines · Daniel P. Berrangé

The following changes since commit 7d3660e79830a069f1848bb4fa1cdf8f666424fb:

Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into stagi=
ng (2020-06-12 23:06:22 +0100)

are available in the Git repository at:

https://github.com/berrange/qemu tags/qcrypto-next-pull-request

for you to fetch changes up to d6cca8e111696fbbd7c233dc53f9c80b6a43359d:

Quoted 9 lines

crypto: Remove use of GCRYPT_VERSION macro. (2020-06-15 11:33:51 +0100)

----------------------------------------------------------------
Misc crypto subsystem fixes

* Improve error message for large files when creating LUKS volumes
* Expand crypto hash benchmark coverage
* Misc code refactoring with no functional change

Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/5.1
for any user-visible changes.

-- PMM