qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Gonglei <arei.gonglei@huawei.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	qemu-devel@nongnu.org, Gerd Hoffmann <kraxel@redhat.com>
Subject: Re: [Qemu-devel] [PATCH 09/10] block: convert qcow/qcow2 to use generic cipher API
Date: Mon, 1 Jun 2015 17:58:33 +0100	[thread overview]
Message-ID: <20150601165833.GE17374@redhat.com> (raw)
In-Reply-To: <55681261.5020907@huawei.com>

On Fri, May 29, 2015 at 03:16:49PM +0800, Gonglei wrote:
> On 2015/5/21 18:56, Daniel P. Berrange wrote:
> > Switch the qcow/qcow2 block driver over to use the generic cipher
> > API, this allows it to use the pluggable AES implementations,
> > instead of being hardcoded to use QEMU's built-in impl.
> > 
> > Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> > ---
> >  block/qcow.c          | 100 ++++++++++++++++++++++++++++++++++++--------------
> >  block/qcow2-cluster.c |  46 ++++++++++++++++++-----
> >  block/qcow2.c         |  94 ++++++++++++++++++++++++-----------------------
> >  block/qcow2.h         |  13 +++----
> >  4 files changed, 162 insertions(+), 91 deletions(-)
> > 
> > diff --git a/block/qcow.c b/block/qcow.c
> > index 50dbcee..7338d1d 100644
> > --- a/block/qcow.c
> > +++ b/block/qcow.c
> > @@ -25,7 +25,7 @@
> >  #include "block/block_int.h"
> >  #include "qemu/module.h"
> >  #include <zlib.h>
> > -#include "crypto/aes.h"
> > +#include "crypto/cipher.h"
> >  #include "migration/migration.h"
> >  
> >  /**************************************************************/
> > @@ -71,10 +71,8 @@ typedef struct BDRVQcowState {
> >      uint8_t *cluster_cache;
> >      uint8_t *cluster_data;
> >      uint64_t cluster_cache_offset;
> > -    uint32_t crypt_method; /* current crypt method, 0 if no key yet */
> > +    QCryptoCipher *cipher; /* NULL if no key yet */
> >      uint32_t crypt_method_header;
> > -    AES_KEY aes_encrypt_key;
> > -    AES_KEY aes_decrypt_key;
> >      CoMutex lock;
> >      Error *migration_blocker;
> >  } BDRVQcowState;
> > @@ -153,6 +151,11 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
> >          ret = -EINVAL;
> >          goto fail;
> >      }
> > +    if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES)) {
> > +        error_setg(errp, "AES cipher not available");
> > +        ret = -EINVAL;
> > +        goto fail;
> > +    }
> >      s->crypt_method_header = header.crypt_method;
> >      if (s->crypt_method_header) {
> >          bs->encrypted = 1;
> > @@ -259,6 +262,7 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
> >      BDRVQcowState *s = bs->opaque;
> >      uint8_t keybuf[16];
> >      int len, i;
> > +    Error *err;
> >  
> >      memset(keybuf, 0, 16);
> >      len = strlen(key);
> > @@ -269,38 +273,66 @@ static int qcow_set_key(BlockDriverState *bs, const char *key)
> >      for(i = 0;i < len;i++) {
> >          keybuf[i] = key[i];
> >      }
> > -    s->crypt_method = s->crypt_method_header;
> >  
> > -    if (AES_set_encrypt_key(keybuf, 128, &s->aes_encrypt_key) != 0)
> > -        return -1;
> > -    if (AES_set_decrypt_key(keybuf, 128, &s->aes_decrypt_key) != 0)
> > +    if (s->cipher) {
> 
> This above check is superfluous.

Hmm, yes, the free method accepts NULL just fine.

> 
> > +        qcrypto_cipher_free(s->cipher);
> > +    }
> > +    s->cipher = qcrypto_cipher_new(
> > +        QCRYPTO_CIPHER_ALG_AES,
> > +        QCRYPTO_CIPHER_MODE_CBC,
> > +        keybuf, G_N_ELEMENTS(keybuf),
> > +        &err);
> > +
> > +    if (!s->cipher) {
> > +        error_free(err);
> 
> Maybe we should report the error message before free it.
> It's the same for below error handling.

We're limited by the error code abilities of this code - basically
there is no where to propagate the error back up to. As & when
this code is updated to properly propagate errors we can do this
here.

> > @@ -1455,6 +1463,8 @@ static void qcow2_close(BlockDriverState *bs)
> >      qcow2_cache_destroy(bs, s->l2_table_cache);
> >      qcow2_cache_destroy(bs, s->refcount_block_cache);
> >  
> > +    qcrypto_cipher_free(s->cipher);
> > +
> 
> Do we need to set s->cipher = NULL ?

Yes, probably worth while as a sanity check.

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2015-06-01 16:58 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-21 10:56 [Qemu-devel] [PATCH 00/10] Consolidate crypto APIs & implementations Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 01/10] crypto: introduce new module for computing hash digests Daniel P. Berrange
2015-05-28 13:28   ` Gonglei
2015-06-01 16:46     ` Daniel P. Berrange
2015-06-02  7:43       ` Markus Armbruster
2015-06-02  8:34         ` Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 02/10] crypto: move built-in AES implementation into crypto/ Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 03/10] crypto: move built-in D3DES " Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 04/10] crypto: introduce generic cipher API & built-in implementation Daniel P. Berrange
2015-05-21 19:52   ` Richard Henderson
2015-05-22  9:10     ` Daniel P. Berrange
2015-05-29  2:39       ` Gonglei
2015-06-01 16:50         ` Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 05/10] crypto: add a gcrypt cipher implementation Daniel P. Berrange
2015-05-29  3:53   ` Gonglei
2015-06-01 16:53     ` Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 06/10] crypto: add a nettle " Daniel P. Berrange
2015-05-21 19:35   ` Richard Henderson
2015-05-29  6:36     ` Gonglei
2015-05-21 19:38   ` Richard Henderson
2015-05-22  9:05     ` Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 07/10] block: convert quorum blockdrv to use crypto APIs Daniel P. Berrange
2015-05-29  6:49   ` Gonglei
2015-06-01 16:56     ` Daniel P. Berrange
2015-05-21 10:56 ` [Qemu-devel] [PATCH 08/10] ui: convert VNC websockets " Daniel P. Berrange
2015-05-29  6:55   ` Gonglei
2015-05-21 10:56 ` [Qemu-devel] [PATCH 09/10] block: convert qcow/qcow2 to use generic cipher API Daniel P. Berrange
2015-05-29  7:16   ` Gonglei
2015-06-01 16:58     ` Daniel P. Berrange [this message]
2015-05-21 10:56 ` [Qemu-devel] [PATCH 10/10] ui: convert VNC " Daniel P. Berrange
2015-05-21 12:51   ` Eric Blake
2015-06-01 16:58     ` Daniel P. Berrange
2015-05-22 11:29 ` [Qemu-devel] [PATCH 00/10] Consolidate crypto APIs & implementations Gonglei
2015-05-22 11:37   ` Daniel P. Berrange
2015-05-22 11:50     ` Gonglei
2015-05-22 12:12       ` Daniel P. Berrange

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150601165833.GE17374@redhat.com \
    --to=berrange@redhat.com \
    --cc=arei.gonglei@huawei.com \
    --cc=kraxel@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).