All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: "Longpeng (Mike)" <longpeng2@huawei.com>
Cc: "Gonglei (Arei)" <arei.gonglei@huawei.com>,
	QEMU-DEV <qemu-devel@nongnu.org>,
	"Wubin (H)" <wu.wubin@huawei.com>
Subject: Re: [Qemu-devel] Question about add AF_ALG backend for virtio-crypto
Date: Wed, 8 Feb 2017 10:53:36 +0000	[thread overview]
Message-ID: <20170208105336.GH3129@redhat.com> (raw)
In-Reply-To: <589AF6EC.5050803@huawei.com>

On Wed, Feb 08, 2017 at 06:46:04PM +0800, Longpeng (Mike) wrote:
> Hi Daniel,
> 
> I was writing AF_ALG-backed for QEMU crypto these days, I think there're more
> than two ways to implements it.
> 
> The first one look likes below:
> [ cipher.c ]
> qcrypto_cipher_new(...)
> {
> 	if (...) { /* use AF_ALG */
> 		cipher = afalg_cipher_new(...)
> 		if (cipher) {
> 			return cipher;
> 		}
> 	}
> 	
> 	/* disabled AF_ALG or AF_ALG failed, then back to
> 	 * using 'builtin'(gcrypt/nettle/...)
> 	 */
> 	cipher = __qcrypto_cipher_new(...)
> }
> 
> [ cipher-afalg.c ]
> afalg_cipher_new(...) {....}
> afalg_cipher_encrypt(...) {...}
> ......
> 
> [ cipher-gcrypt.c ]
> __qcrypto_cipher_new(...) {...}
> __qcrypto_cipher_encrypt(...) {...}
> ......
> 
> [ cipher-nettle.c ]
> __qcrypto_cipher_new(...) {...}
> __qcrypto_cipher_encrypt(...) {...}
> ......
> 
> In this way, I think I need to rename most functions in
> cipher-gcrypt.c/cipher-nettle.c with a prefixion(such as '__')
> 
> 
> Alternative way is:
> [ cipher-afalg.c ]
> afalg_cipher_new(...) {....}
> afalg_cipher_encrypt(...) {...}
> ......
> 
> [ cipher-gcrypt.c ]
> qcrypto_cipher_new(...)
> {
> 	if (...) { /* use AF_ALG */
> 		cipher = afalg_cipher_new(...)
> 		if (cipher) {
> 			return cipher;
> 		}
> 	}
> 	
> 	/* disabled AF_ALG or AF_ALG failed, then back to
> 	 * using 'builtin'
> 	 */
> 	.......( the existing code )
> }
> ......
> 
> [ cipher-nettle.c ]
> qcrypto_cipher_new(...)
> {
> 	if (...) { /* use AF_ALG */
> 		cipher = afalg_cipher_new(...)
> 		if (cipher) {
> 			return cipher;
> 		}
> 	}
> 	
> 	/* disabled AF_ALG or AF_ALG failed, then back to
> 	 * using 'builtin'
> 	 */
> 	.......( the existing code )
> }
> ......
> 
> In this way, we should add AF_ALG-backed code in most functions in
> cipher-gcrypt.c/cipher-nettle.c, I'm afraid this would introduce lots of
> duplicate code because the same AF_ALG-backed code must in both gcrypt-backed
> impls and nettle-backed impls as above.
> 
> I'm confusing about which way you'd prefer, or do you have any better
> suggestion?

Yeah, both approaches have some reasonably significant downsides. Approach
1 is sort of like providing a virtual driver table, except it is hardcoded
to switch between 2 impls only.

A variant on approach 1 is to actually setup a proper driver-table dispatch
layer. eg define a struct that contains callbacks for each public api
operation. The qcrypto_cipher_new() method will then either setup callbacks
for AF_ALG, or for the library impl.

This is the design we took in crypto/{ivgen.c,ivgenpriv.h}


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

  reply	other threads:[~2017-02-08 10:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-09  7:04 [Qemu-devel] Question about add AF_ALG backend for virtio-crypto Longpeng (Mike)
2017-01-09 13:43 ` Stefan Hajnoczi
2017-01-09 16:25   ` Daniel P. Berrange
2017-01-10  9:03     ` Gonglei (Arei)
2017-01-10 10:03       ` Daniel P. Berrange
2017-01-10 11:36         ` Gonglei (Arei)
2017-01-10 12:03           ` Daniel P. Berrange
2017-01-10 12:17             ` Gonglei (Arei)
2017-01-10 13:30               ` Daniel P. Berrange
2017-02-08 10:46                 ` Longpeng (Mike)
2017-02-08 10:53                   ` Daniel P. Berrange [this message]
2017-02-09  2:58                     ` Longpeng (Mike)
2017-02-09 10:11                       ` Daniel P. Berrange
2017-02-09 11:03                         ` Longpeng (Mike)
2017-02-09 11:22                           ` 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=20170208105336.GH3129@redhat.com \
    --to=berrange@redhat.com \
    --cc=arei.gonglei@huawei.com \
    --cc=longpeng2@huawei.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wu.wubin@huawei.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.