Skip to contentSkip to conversation

encryption code changes

PATCH In review Eduardo Habkost 5 participants 17 messages Feb 6, 2009 – Feb 18, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

cover encryption code changes

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

Signature · 1 line
Eduardo
#1 lore raw

PATCH 1/9 vnc: abort on unknown options

Currently, if the user specifies an authentication mode that is not compiled in, qemu silently falls back to disabling authentication.

Aborting instead of falling back to an unsafe mode is the right thing to do.

---
 vnc.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
vnc.c +3 -0
index 68df599..5c2368c 100644
@@ -2441,6 +2441,9 @@ int vnc_display_open(DisplayState *ds, const char *display)
 		return -1;
 	    }
 #endif
+	} else {
+	    fprintf(stderr, "Invalid vnc option: %s\n", options);
+	    return -1;
 	}
     }
 
Signature · 1 line
1.6.0.2.GIT
#2 lore raw

PATCH 2/9 drive_init: Don't try to read passwords before monitor setup

In reply to Eduardo Habkost

drive_init() calls qemu_key_check(), but it doesn't make sense to call it before the monitor was set up. This makes qemu hang forever waiting for a password on a monitor that doesn't exist.

Removing that call will make the password to be read at a proper time, at read_passwords().

---
 vl.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
vl.c +1 -1
index aff2b2c..d3ec724 100644
@@ -2548,7 +2548,7 @@ static int drive_init(struct drive_opt *arg, int snapshot,
         bdrv_flags |= BDRV_O_CACHE_WB;
     else if (cache == 3) /* not specified */
         bdrv_flags |= BDRV_O_CACHE_DEF;
-    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
+    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0) {
         fprintf(stderr, "qemu: could not open disk image %s\n",
                         file);
         return -1;
Signature · 1 line
1.6.0.2.GIT
#3 lore raw

PATCH 3/9 monitor_readline: poll pending bottom halves before readline_start()

In reply to Eduardo Habkost

monitor_readline() breaks when a CHR_EVENT_RESET event bottom-half is pending, because the bottom-half resets the console readline state.

Call qemu_bh_poll() before readline_start() to avoid that from happening.

Quoted 1 line

From my tests, there is more monitor_readline() badness on the current

code, but this should make things slightly better.

---
 monitor.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
monitor.c +6 -0
index 354a792..138e829 100644
@@ -2862,6 +2862,12 @@ void monitor_readline(const char *prompt, int is_password,
         }
     }
 
+    /* Terminal reset events may be pending, and they would
+     * drop the setup done by the readline_start() call
+     * below.
+     */
+    qemu_bh_poll();
+
     readline_start(prompt, is_password, monitor_readline_cb, NULL);
     monitor_readline_buf = buf;
     monitor_readline_buf_size = buf_size;
Signature · 1 line
1.6.0.2.GIT
#4 lore raw

PATCH 4/9 qcow: define QCOW_CRYPT_MAX

In reply to Eduardo Habkost

Just to make the code a bit more readable, and get ready to make AES optional.

---
 block-qcow.c  |    3 ++-
 block-qcow2.c |    3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
block-qcow.c +2 -1
index 91c53b1..4fdd0d8 100644
@@ -34,6 +34,7 @@
 
 #define QCOW_CRYPT_NONE 0
 #define QCOW_CRYPT_AES  1
+#define QCOW_CRYPT_MAX  QCOW_CRYPT_AES
 
 #define QCOW_OFLAG_COMPRESSED (1LL << 63)
 
@@ -113,7 +114,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
         goto fail;
     if (header.size <= 1 || header.cluster_bits < 9)
         goto fail;
-    if (header.crypt_method > QCOW_CRYPT_AES)
+    if (header.crypt_method > QCOW_CRYPT_MAX)
         goto fail;
     s->crypt_method_header = header.crypt_method;
     if (s->crypt_method_header)
block-qcow2.c +2 -1
index 465dcd6..d1503e9 100644
@@ -51,6 +51,7 @@
 
 #define QCOW_CRYPT_NONE 0
 #define QCOW_CRYPT_AES  1
+#define QCOW_CRYPT_MAX  QCOW_CRYPT_AES
 
 #define QCOW_MAX_CRYPT_CLUSTERS 32
 
@@ -228,7 +229,7 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
         header.cluster_bits < 9 ||
         header.cluster_bits > 16)
         goto fail;
-    if (header.crypt_method > QCOW_CRYPT_AES)
+    if (header.crypt_method > QCOW_CRYPT_MAX)
         goto fail;
     s->crypt_method_header = header.crypt_method;
     if (s->crypt_method_header)
Signature · 1 line
1.6.0.2.GIT
#5 lore raw

PATCH 5/9 qcow: make encryption support optional

In reply to Eduardo Habkost

I will change it to use AES encryption support from libgcrypt, and making it optional will allow qcow to be compiled if libgcrypt is not available.

---
 block-qcow.c  |   41 ++++++++++++++++++++++++++++++++++++++---
 block-qcow2.c |   45 +++++++++++++++++++++++++++++++++++++++++----
 configure     |    8 ++++++++
 3 files changed, 87 insertions(+), 7 deletions(-)
block-qcow.c +38 -3
index 4fdd0d8..a283fa2 100644
@@ -24,7 +24,10 @@
 #include "qemu-common.h"
 #include "block_int.h"
 #include <zlib.h>
+
+#ifdef CONFIG_QCOW_AES
 #include "aes.h"
+#endif
 
 /**************************************************************/
 /* QEMU COW block driver with compression and encryption support */
@@ -72,8 +75,10 @@ typedef struct BDRVQcowState {
     uint64_t cluster_cache_offset;
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
     uint32_t crypt_method_header;
+#ifdef CONFIG_QCOW_AES
     AES_KEY aes_encrypt_key;
     AES_KEY aes_decrypt_key;
+#endif
 } BDRVQcowState;
 
 static int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
@@ -116,6 +121,10 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
         goto fail;
     if (header.crypt_method > QCOW_CRYPT_MAX)
         goto fail;
+#ifndef CONFIG_QCOW_AES
+    if (header.crypt_method == QCOW_CRYPT_AES)
+        goto fail;
+#endif
     s->crypt_method_header = header.crypt_method;
     if (s->crypt_method_header)
         bs->encrypted = 1;
@@ -173,7 +182,8 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
     return -1;
 }
 
-static int qcow_set_key(BlockDriverState *bs, const char *key)
+#ifdef CONFIG_QCOW_AES
+static int qcow_set_key_aes(BlockDriverState *bs, const char *key)
 {
     BDRVQcowState *s = bs->opaque;
     uint8_t keybuf[16];
@@ -214,7 +224,19 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
 #endif
     return 0;
 }
+#endif
+
+static int qcow_set_key(BlockDriverState *bs, const char *key)
+{
+#ifdef CONFIG_QCOW_AES
+    return qcow_set_key_aes(bs, key);
+#else
+    return -1;
+#endif
+}
+
 
+#ifdef CONFIG_QCOW_AES
 /* The crypt function is compatible with the linux cryptoloop
    algorithm for < 4 GB images. NOTE: out_buf == in_buf is
    supported */
@@ -239,6 +261,7 @@ static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
         out_buf += 512;
     }
 }
+#endif
 
 /* 'allocate' is:
  *
@@ -345,6 +368,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
                 cluster_offset = (cluster_offset + s->cluster_size - 1) &
                     ~(s->cluster_size - 1);
                 bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
+#ifdef CONFIG_QCOW_AES
                 /* if encrypted, we must initialize the cluster
                    content which won't be written */
                 if (s->crypt_method &&
@@ -364,6 +388,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
                         }
                     }
                 }
+#endif /* CONFIG_QCOW_AES */
             } else if (allocate == 2) {
                 cluster_offset |= QCOW_OFLAG_COMPRESSED |
                     (uint64_t)compressed_size << (63 - s->cluster_bits);
@@ -505,12 +530,16 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num,
                                             index_in_cluster + n);
         if (!cluster_offset)
             return -1;
+#ifdef CONFIG_QCOW_AES
         if (s->crypt_method) {
             encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
                             &s->aes_encrypt_key);
             ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
                               s->cluster_data, n * 512);
-        } else {
+        }
+        else
+#endif /* CONFIG_QCOW_AES */
+        {
             ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
         }
         if (ret != n * 512)
@@ -556,11 +585,13 @@ static void qcow_aio_read_cb(void *opaque, int ret)
     } else if (acb->cluster_offset & QCOW_OFLAG_COMPRESSED) {
         /* nothing to do */
     } else {
+#ifdef CONFIG_QCOW_AES
         if (s->crypt_method) {
             encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
                             acb->n, 0,
                             &s->aes_decrypt_key);
         }
+#endif
     }
 
     acb->nb_sectors -= acb->n;
@@ -674,6 +705,7 @@ static void qcow_aio_write_cb(void *opaque, int ret)
         ret = -EIO;
         goto fail;
     }
+#ifdef CONFIG_QCOW_AES
     if (s->crypt_method) {
         if (!acb->cluster_data) {
             acb->cluster_data = qemu_mallocz(s->cluster_size);
@@ -685,7 +717,10 @@ static void qcow_aio_write_cb(void *opaque, int ret)
         encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
                         acb->n, 1, &s->aes_encrypt_key);
         src_buf = acb->cluster_data;
-    } else {
+    }
+    else
+#endif /* CONFIG_QCOW_AES */
+    {
         src_buf = acb->buf;
     }
     acb->hd_aiocb = bdrv_aio_write(s->hd,
block-qcow2.c +41 -4

142 lines not on this page · raw message

configure +8 -0
index c3fbbbe..604055c 100755
@@ -164,6 +164,7 @@ fmod_lib=""
 fmod_inc=""
 oss_lib=""
 vnc_tls="yes"
+qcow_aes="yes"
 bsd="no"
 linux="no"
 solaris="no"
@@ -387,6 +388,8 @@ for opt do
   ;;
   --disable-vnc-tls) vnc_tls="no"
   ;;
+  --disable-qcow-aes) qcow_aes="no"
+  ;;
   --disable-slirp) slirp="no"
   ;;
   --disable-vde) vde="no"
@@ -544,6 +547,7 @@ echo "                           Available cards: $audio_possible_cards"
 echo "  --enable-mixemu          enable mixer emulation"
 echo "  --disable-brlapi         disable BrlAPI"
 echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
+echo "  --disable-qcow-aes       disable AES encrypton support on qcow"
 echo "  --disable-curses         disable curses output"
 echo "  --disable-bluez          disable bluez stack connectivity"
 echo "  --disable-kvm            disable KVM acceleration support"
@@ -1130,6 +1134,7 @@ if test "$vnc_tls" = "yes" ; then
     echo "    TLS CFLAGS    $vnc_tls_cflags"
     echo "    TLS LIBS      $vnc_tls_libs"
 fi
+echo "qcow encryption   $qcow_aes"
 if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
 fi
@@ -1371,6 +1376,9 @@ if test "$vnc_tls" = "yes" ; then
   echo "CONFIG_VNC_TLS_LIBS=$vnc_tls_libs" >> $config_mak
   echo "#define CONFIG_VNC_TLS 1" >> $config_h
 fi
+if [ "$qcow_aes" = "yes" ];then
+  echo "#define CONFIG_QCOW_AES 1" >> $config_h
+fi
 qemu_version=`head $source_path/VERSION`
 echo "VERSION=$qemu_version" >>$config_mak
 echo "#define QEMU_VERSION \"$qemu_version\"" >> $config_h
Signature · 1 line
1.6.0.2.GIT
#6 lore raw

PATCH 6/9 vnc: make DES-challenge authentication (aka "VNC auth") optional

In reply to Eduardo Habkost

I will change it to use libgcrypt, and making it optional will allow vnc.c to be compiled if libgcrypt is not available.

---
 Makefile  |   15 +++++++++++++--
 configure |    9 +++++++++
 vnc.c     |   43 ++++++++++++++++++++++++++++++++++---------
 3 files changed, 56 insertions(+), 11 deletions(-)
Makefile +13 -2
index 4f7a55a..98f67b1 100644
@@ -143,7 +143,12 @@ endif
 ifdef CONFIG_CURSES
 OBJS+=curses.o
 endif
-OBJS+=vnc.o d3des.o
+
+OBJS+=vnc.o
+
+ifdef CONFIG_VNC_DES
+OBJS+=d3des.o
+endif
 
 ifdef CONFIG_COCOA
 OBJS+=cocoa.o
@@ -165,7 +170,13 @@ sdl.o: sdl.c keymaps.c sdl_keysym.h
 
 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
 
-vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h
+VNC_FILES = vnc.c keymaps.c sdl_keysym.h vnchextile.h
+
+ifdef CONFIG_VNC_DES
+VNC_FILES += d3des.c d3des.h
+endif
+
+vnc.o: $(VNC_FILES)
 
 vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
 
configure +9 -0
index 604055c..d2aa783 100755
@@ -165,6 +165,7 @@ fmod_inc=""
 oss_lib=""
 vnc_tls="yes"
 qcow_aes="yes"
+vnc_des="yes"
 bsd="no"
 linux="no"
 solaris="no"
@@ -390,6 +391,8 @@ for opt do
   ;;
   --disable-qcow-aes) qcow_aes="no"
   ;;
+  --disable-vnc-des) vnc_des="no"
+  ;;
   --disable-slirp) slirp="no"
   ;;
   --disable-vde) vde="no"
@@ -548,6 +551,7 @@ echo "  --enable-mixemu          enable mixer emulation"
 echo "  --disable-brlapi         disable BrlAPI"
 echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
 echo "  --disable-qcow-aes       disable AES encrypton support on qcow"
+echo "  --disable-vnc-des        disable VNC DES-challenge authentication"
 echo "  --disable-curses         disable curses output"
 echo "  --disable-bluez          disable bluez stack connectivity"
 echo "  --disable-kvm            disable KVM acceleration support"
@@ -1134,6 +1138,7 @@ if test "$vnc_tls" = "yes" ; then
     echo "    TLS CFLAGS    $vnc_tls_cflags"
     echo "    TLS LIBS      $vnc_tls_libs"
 fi
+echo "VNC DES auth      $vnc_des"
 echo "qcow encryption   $qcow_aes"
 if test -n "$sparc_cpu"; then
     echo "Target Sparc Arch $sparc_cpu"
@@ -1376,6 +1381,10 @@ if test "$vnc_tls" = "yes" ; then
   echo "CONFIG_VNC_TLS_LIBS=$vnc_tls_libs" >> $config_mak
   echo "#define CONFIG_VNC_TLS 1" >> $config_h
 fi
+if [ "$vnc_des" = "yes" ];then
+  echo "CONFIG_VNC_DES=yes" >> $config_mak
+  echo "#define CONFIG_VNC_DES 1" >> $config_h
+fi
 if [ "$qcow_aes" = "yes" ];then
   echo "#define CONFIG_QCOW_AES 1" >> $config_h
 fi
vnc.c +34 -9

139 lines not on this page · raw message

Signature · 1 line
1.6.0.2.GIT
#7 lore raw

PATCH 7/9 configure: add check for libgcrypt

In reply to Eduardo Habkost
---
 Makefile        |    6 ++++++
 Makefile.target |    5 +++++
 configure       |   37 +++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 0 deletions(-)
Makefile +6 -0
index 98f67b1..f634d1a 100644
@@ -162,6 +162,12 @@ tcp_subr.o tcp_timer.o udp.o bootp.o debug.o tftp.o
 OBJS+=$(addprefix slirp/, $(SLIRP_OBJS))
 endif
 
+ifdef CONFIG_GCRYPT
+CFLAGS += $(CONFIG_GCRYPT_CFLAGS)
+LIBS += $(CONFIG_GCRYPT_LIBS)
+endif
+
+
 LIBS+=$(VDE_LIBS)
 
 cocoa.o: cocoa.m
Makefile.target +5 -0
index 28ba17f..f6182fe 100644
@@ -554,6 +554,11 @@ CPPFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
 LIBS += $(CONFIG_VNC_TLS_LIBS)
 endif
 
+ifdef CONFIG_GCRYPT
+CPPFLAGS += $(CONFIG_GCRYPT_CFLAGS)
+LIBS += $(CONFIG_GCRYPT_LIBS)
+endif
+
 ifdef CONFIG_BLUEZ
 LIBS += $(CONFIG_BLUEZ_LIBS)
 endif
configure +37 -0
index d2aa783..d38d99e 100755
@@ -164,6 +164,7 @@ fmod_lib=""
 fmod_inc=""
 oss_lib=""
 vnc_tls="yes"
+libgcrypt="yes"
 qcow_aes="yes"
 vnc_des="yes"
 bsd="no"
@@ -389,6 +390,8 @@ for opt do
   ;;
   --disable-vnc-tls) vnc_tls="no"
   ;;
+  --disable-gcrypt) libgcrypt="no"
+  ;;
   --disable-qcow-aes) qcow_aes="no"
   ;;
   --disable-vnc-des) vnc_des="no"
@@ -550,6 +553,7 @@ echo "                           Available cards: $audio_possible_cards"
 echo "  --enable-mixemu          enable mixer emulation"
 echo "  --disable-brlapi         disable BrlAPI"
 echo "  --disable-vnc-tls        disable TLS encryption for VNC server"
+echo "  --disable-gcrypt         disable libgcrypt usage"
 echo "  --disable-qcow-aes       disable AES encrypton support on qcow"
 echo "  --disable-vnc-des        disable VNC DES-challenge authentication"
 echo "  --disable-curses         disable curses output"
@@ -831,6 +835,28 @@ EOF
 fi
 
 ##########################################
+# libgcrypt detection
+if test "$libgcrypt" = "yes" ; then
+cat > $TMPC <<EOF
+#include <gcrypt.h>
+int main(void) {
+  gcry_cipher_hd_t ci;
+  gcry_error_t e = gcry_cipher_open(&ci, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
+  return 0;
+}
+EOF
+    libgcrypt_cflags=`libgcrypt-config --cflags 2> /dev/null`
+    libgcrypt_libs=`libgcrypt-config --libs 2> /dev/null`
+    if $cc $ARCH_CFLAGS -o $TMPE ${OS_CFLAGS} $libgcrypt_cflags $TMPC \
+           $libgcrypt_libs > /dev/null 2> /dev/null ; then
+	:
+    else
+	libgcrypt="no"
+    fi
+fi
+
+
+##########################################
 # vde libraries probe
 if test "$vde" = "yes" ; then
   cat > $TMPC << EOF
@@ -1133,6 +1159,11 @@ echo "mingw32 support   $mingw32"
 echo "Audio drivers     $audio_drv_list"
 echo "Extra audio cards $audio_card_list"
 echo "Mixer emulation   $mixemu"
+echo "libgcrypt support $libgcrypt"
+if test "$libgcrypt" = "yes" ; then
+    echo "   gcrypt CFLAGS  $libgcrypt_cflags"
+    echo "   gcrypt LIBS    $libgcrypt_libs"
+fi
 echo "VNC TLS support   $vnc_tls"
 if test "$vnc_tls" = "yes" ; then
     echo "    TLS CFLAGS    $vnc_tls_cflags"
@@ -1385,6 +1416,12 @@ if [ "$vnc_des" = "yes" ];then
   echo "CONFIG_VNC_DES=yes" >> $config_mak
   echo "#define CONFIG_VNC_DES 1" >> $config_h
 fi
+if test "$libgcrypt" = "yes" ; then
+  echo "CONFIG_GCRYPT=yes" >> $config_mak
+  echo "CONFIG_GCRYPT_CFLAGS=$libgcrypt_cflags" >> $config_mak
+  echo "CONFIG_GCRYPT_LIBS=$libgcrypt_libs" >> $config_mak
+  echo "#define CONFIG_GCRYPT 1" >> $config_h
+fi
 if [ "$qcow_aes" = "yes" ];then
   echo "#define CONFIG_QCOW_AES 1" >> $config_h
 fi
Signature · 1 line
1.6.0.2.GIT
#8 lore raw

PATCH 8/9 qcow: use libgcrypt AES implementation

In reply to Eduardo Habkost

To keep the qcow code simple, keep an API as close as possible to the old openssl one, wrapping the libgcrypt API. This wrapper API is on aes.h (as inline functions).

Changed configure to disable qcow AES encryption support if libgcrypt is disabled or not found.

---
 Makefile      |    2 +-
 aes.c         | 1320 ---------------------------------------------------------
 aes.h         |  112 ++++--
 block-qcow.c  |   36 ++-
 block-qcow2.c |   41 ++-
 configure     |    6 +
 6 files changed, 147 insertions(+), 1370 deletions(-)
 delete mode 100644 aes.c
Makefile +1 -1
index f634d1a..b565b36 100644
@@ -50,7 +50,7 @@ recurse-all: $(SUBDIR_RULES)
 # BLOCK_OBJS is code used by both qemu system emulation and qemu-img
 
 BLOCK_OBJS=cutils.o qemu-malloc.o
-BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o
+BLOCK_OBJS+=block-cow.o block-qcow.o block-vmdk.o block-cloop.o
 BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
 BLOCK_OBJS+=block-qcow2.o block-parallels.o block-nbd.o
 BLOCK_OBJS+=nbd.o block.o aio.o
aes.c deleted +0 -1320

1,320 lines not on this page · raw message

aes.h +86 -26

112 lines not on this page · raw message

block-qcow.c +25 -11
index a283fa2..98bd2b8 100644
@@ -76,8 +76,8 @@ typedef struct BDRVQcowState {
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
     uint32_t crypt_method_header;
 #ifdef CONFIG_QCOW_AES
-    AES_KEY aes_encrypt_key;
-    AES_KEY aes_decrypt_key;
+    AES_CBC_CIPHER aes_encrypt_cipher;
+    AES_CBC_CIPHER aes_decrypt_cipher;
 #endif
 } BDRVQcowState;
 
@@ -171,6 +171,16 @@ static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
             goto fail;
         bs->backing_file[len] = '\0';
     }
+
+#ifdef CONFIG_QCOW_AES
+    if (header.crypt_method == QCOW_CRYPT_AES) {
+        if (AES_CBC_init(&s->aes_encrypt_cipher))
+            goto fail;
+        if (AES_CBC_init(&s->aes_decrypt_cipher))
+            goto fail;
+    }
+#endif
+
     return 0;
 
  fail:
@@ -200,9 +210,9 @@ static int qcow_set_key_aes(BlockDriverState *bs, const char *key)
     }
     s->crypt_method = s->crypt_method_header;
 
-    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
+    if (AES_CBC_set_key(keybuf, 128, &s->aes_encrypt_cipher) != 0)
         return -1;
-    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
+    if (AES_CBC_set_key(keybuf, 128, &s->aes_decrypt_cipher) != 0)
         return -1;
 #if 0
     /* test */
@@ -243,7 +253,7 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
 static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
                             uint8_t *out_buf, const uint8_t *in_buf,
                             int nb_sectors, int enc,
-                            const AES_KEY *key)
+                            const AES_CBC_CIPHER *key)
 {
     union {
         uint64_t ll[2];
@@ -254,8 +264,8 @@ static void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
     for(i = 0; i < nb_sectors; i++) {
         ivec.ll[0] = cpu_to_le64(sector_num);
         ivec.ll[1] = 0;
-        AES_cbc_encrypt(in_buf, out_buf, 512, key,
-                        ivec.b, enc);
+        AES_CBC_encrypt(in_buf, out_buf, 512, key,
+                        ivec.b, 16, enc);
         sector_num++;
         in_buf += 512;
         out_buf += 512;
@@ -381,7 +391,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
                             encrypt_sectors(s, start_sect + i,
                                             s->cluster_data,
                                             s->cluster_data + 512, 1, 1,
-                                            &s->aes_encrypt_key);
+                                            &s->aes_encrypt_cipher);
                             if (bdrv_pwrite(s->hd, cluster_offset + i * 512,
                                             s->cluster_data, 512) != 512)
                                 return -1;
@@ -533,7 +543,7 @@ static int qcow_write(BlockDriverState *bs, int64_t sector_num,
 #ifdef CONFIG_QCOW_AES
         if (s->crypt_method) {
             encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
-                            &s->aes_encrypt_key);
+                            &s->aes_encrypt_cipher);
             ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512,
                               s->cluster_data, n * 512);
         }
@@ -589,7 +599,7 @@ static void qcow_aio_read_cb(void *opaque, int ret)
         if (s->crypt_method) {
             encrypt_sectors(s, acb->sector_num, acb->buf, acb->buf,
                             acb->n, 0,
-                            &s->aes_decrypt_key);
+                            &s->aes_decrypt_cipher);
         }
 #endif
     }
@@ -715,7 +725,7 @@ static void qcow_aio_write_cb(void *opaque, int ret)
             }
         }
         encrypt_sectors(s, acb->sector_num, acb->cluster_data, acb->buf,
-                        acb->n, 1, &s->aes_encrypt_key);
+                        acb->n, 1, &s->aes_encrypt_cipher);
         src_buf = acb->cluster_data;
     }
     else
@@ -764,6 +774,10 @@ static void qcow_aio_cancel(BlockDriverAIOCB *blockacb)
 static void qcow_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
+#ifdef CONFIG_QCOW_AES
+    AES_CBC_deinit(&s->aes_encrypt_cipher);
+    AES_CBC_deinit(&s->aes_decrypt_cipher);
+#endif
     qemu_free(s->l1_table);
     qemu_free(s->l2_cache);
     qemu_free(s->cluster_cache);
block-qcow2.c +29 -12

114 lines not on this page · raw message

configure +6 -0
index d38d99e..c5b6e90 100755
@@ -856,6 +856,12 @@ EOF
 fi
 
 
+
+if [ "$libgcrypt" = "no" ];then
+    qcow_aes="no (needs libgcrypt)"
+fi
+
+
 ##########################################
 # vde libraries probe
 if test "$vde" = "yes" ; then
Signature · 1 line
1.6.0.2.GIT
#9 lore raw

PATCH 9/9 vnc: use libgcrypt for DES-challenge authentication

In reply to Eduardo Habkost

d3des.c has a hack to reverse the bits on each key byte when setting the key. For using a standard DES implementation, we need to reverse the bits ourselves.

Changed configure to disable VNC DES-challenge authentication if libgcrypt is disabled or not available.

---
 Makefile  |    7 -
 configure |    1 +
 d3des.c   |  424 -------------------------------------------------------------
 d3des.h   |   51 --------
 vnc.c     |   63 ++++++++-
 5 files changed, 58 insertions(+), 488 deletions(-)
 delete mode 100644 d3des.c
 delete mode 100644 d3des.h
Makefile +0 -7
index b565b36..b1f6133 100644
@@ -146,9 +146,6 @@ endif
 
 OBJS+=vnc.o
 
-ifdef CONFIG_VNC_DES
-OBJS+=d3des.o
-endif
 
 ifdef CONFIG_COCOA
 OBJS+=cocoa.o
@@ -178,10 +175,6 @@ sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
 
 VNC_FILES = vnc.c keymaps.c sdl_keysym.h vnchextile.h
 
-ifdef CONFIG_VNC_DES
-VNC_FILES += d3des.c d3des.h
-endif
-
 vnc.o: $(VNC_FILES)
 
 vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
configure +1 -0
index c5b6e90..f839540 100755
@@ -859,6 +859,7 @@ fi
 
 if [ "$libgcrypt" = "no" ];then
     qcow_aes="no (needs libgcrypt)"
+    vnc_des="no (needs libgcrypt)"
 fi
 
 
d3des.c deleted +0 -424

424 lines not on this page · raw message

d3des.h deleted +0 -51

51 lines not on this page · raw message

vnc.c +57 -6

108 lines not on this page · raw message

Signature · 1 line
1.6.0.2.GIT
#10 lore raw
In reply to Eduardo Habkost
Quoted 5 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

Quoted 8 lines

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Don't worry about monitor and readline, I'm reworking it ATM, hope being able to post the result by the end of this WE.

It's true there are more fundamental things broken (VM is blocked while reading passwords, console mux'ing is clumsy), and some things are still broken even with your patches (mounting encrypted USB disks on startup, reading passwords from virtual consoles). Sigh, this all looked trivial to get right on first sight but turned out to be _much_ more freaky...

Jan

#11 lore raw
In reply to Eduardo Habkost
Quoted 14 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Quoted 4 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

What's the availability of libgcrypt? Are there Windows versions readily available? That would be my biggest concern here.

Regards,

Anthony Liguori

#12 lore raw
Quoted 15 lines · Anthony Liguori
Quoted 14 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Quoted 7 lines
Quoted 4 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

What's the availability of libgcrypt? Are there Windows versions readily available? That would be my biggest concern here.

It is available for Windows - we build it in the Fedora MinGW project as it is a pre-requisite for GNUTLS and thus libvirt on Windows. It is also used by (and comes from the) GnuPG project

http://www.gnupg.org/documentation/manuals/gcrypt/

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#13 lore raw
In reply to Anthony Liguori
Quoted 15 lines · Anthony Liguori
Quoted 14 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Quoted 7 lines
Quoted 4 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

What's the availability of libgcrypt? Are there Windows versions readily available? That would be my biggest concern here.

libgcrypt is included on the GnuTLS Windows package. I didn't test the changes under Windows, though.

Signature · 1 line
Eduardo
#14 lore raw
In reply to Eduardo Habkost
Quoted 14 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Quoted 3 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

FYI, for those who don't realize, libgcrypt is the crypto library used by GNUTLS. QEMU already uses GNUTLS for its VNC server, if --with-vnc-tls flag is given to configure. IMHO, using libgcrypt for all crypto is a good idea, although it would become a compulsory dependancy instead of an optional one.

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#15 lore raw
In reply to Eduardo Habkost
Quoted 5 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

Quoted 9 lines

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Can you split these out? Jan's monitor series may fix some of these too.

Quoted 4 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

I'm having a hard time justifying this. We're adding an external dependency but not gaining any features and potentially making existing features unavailable on platforms that lack said dependency. It's going to create confusion and surprise.

I understand using gcrypt allows us to rely on a third party for security/bug fixes but I'm having a hard time seeing the value of that justify the pain this is going to cause a certain class of users. I'm open to persuasion but that's how I'm currently leaning.

Regards,

Anthony Liguori

#16 lore raw

Oops. I've seen this message only today because I was not copied. Comments below.

Excerpts from Anthony Liguori's message of Sáb Fev 14 20:23:38 -0200 2009:

Quoted 9 lines
Quoted 8 lines · Eduardo Habkost

Hi,

This patch series for qemu contain multiple changes on the way encryption and authentication code is handled.

The first patch is a behaviour change to avoid silent security holes on the VNC server caused by user configuration errors.

Quoted 8 lines
Quoted 6 lines

Patches 2 and 3 are bugfixes to some of the multiple problems I had with monitor_readline(), when testing the qcow encryption support. monitor_readline() is still not completely functional, but at least it allows the qcow password to be read when an qcow encrypted image is specified on the command-line, now.

Can you split these out? Jan's monitor series may fix some of these too.

Yes, that part can be simply dropped. I've touched that code just because I needed it to test the qcow encryption changes.

Quoted 11 lines
Quoted 5 lines

The remaining patches may be more controversial. The first half makes the use of aes.c and d3des.c optional at compile time. The rest remove aes.c and d3des.c from the source tree and replace them with calls to libgcrypt.

I'm having a hard time justifying this. We're adding an external dependency but not gaining any features and potentially making existing features unavailable on platforms that lack said dependency. It's going to create confusion and surprise.

Quoted 4 lines

I understand using gcrypt allows us to rely on a third party for security/bug fixes but I'm having a hard time seeing the value of that justify the pain this is going to cause a certain class of users. I'm open to persuasion but that's how I'm currently leaning.

In addition to the benefits of not duplicating code, another motivation for the change is that in some countries the cryptography code can cause additional bureaucracy when redistributing the software.

And, as Daniel Berrange noted, we already optionally link against gnutls, that uses libgcrypto. I think it is reasonable to require it for the qcow AES encryption support. Requiring it for VNC password authentication may be more controversial, as it is a more commonly used feature.

Signature · 1 line
Eduardo