* [Qemu-devel] [PATCH for-2.10 13/19] crypto: introduce some common functions for af_alg backend
@ 2017-04-10 9:00 Longpeng(Mike)
2017-04-10 10:15 ` Daniel P. Berrange
0 siblings, 1 reply; 2+ messages in thread
From: Longpeng(Mike) @ 2017-04-10 9:00 UTC (permalink / raw)
To: berrange; +Cc: xuquan8, arei.gonglei, qemu-devel, Longpeng(Mike)
This patch introduces some common functions for af_alg backend,
they would be used in af_alg-backend cipher/hash/hmac latter.
Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
---
crypto/Makefile.objs | 1 +
crypto/afalg-comm.c | 71 +++++++++++++++++++++++++++++++++++++++++++++
include/crypto/afalg-comm.h | 61 ++++++++++++++++++++++++++++++++++++++
3 files changed, 133 insertions(+)
create mode 100644 crypto/afalg-comm.c
create mode 100644 include/crypto/afalg-comm.h
diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs
index 1f749f2..6f244a3 100644
--- a/crypto/Makefile.objs
+++ b/crypto/Makefile.objs
@@ -10,6 +10,7 @@ crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT_HMAC),n,y)) += hmac-glib
crypto-obj-y += aes.o
crypto-obj-y += desrfb.o
crypto-obj-y += cipher.o
+crypto-obj-$(CONFIG_AF_ALG) += afalg-comm.o
crypto-obj-y += tlscreds.o
crypto-obj-y += tlscredsanon.o
crypto-obj-y += tlscredsx509.o
diff --git a/crypto/afalg-comm.c b/crypto/afalg-comm.c
new file mode 100644
index 0000000..27bc88c
--- /dev/null
+++ b/crypto/afalg-comm.c
@@ -0,0 +1,71 @@
+/*
+ * QEMU Crypto af_alg support
+ *
+ * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
+ *
+ * Authors:
+ * Longpeng(Mike) <longpeng2@huawei.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version. See the COPYING file in the
+ * top-level directory.
+ */
+#include "qemu/osdep.h"
+#include "qemu/cutils.h"
+#include "qemu/sockets.h"
+#include "qapi/error.h"
+#include "crypto/afalg-comm.h"
+
+void afalg_comm_format_type(AfalgSocketAddress *afalg,
+ const char *type)
+{
+ afalg->type = (char *)g_new0(int8_t, SALG_TYPE_LEN_MAX);
+ pstrcpy(afalg->type, SALG_TYPE_LEN_MAX, type);
+}
+
+void afalg_comm_free(QCryptoAfalg *afalg)
+{
+ if (afalg) {
+ if (afalg->msg) {
+ g_free(afalg->msg->msg_control);
+ g_free(afalg->msg);
+ }
+
+ if (afalg->tfmfd != -1) {
+ closesocket(afalg->tfmfd);
+ }
+
+ if (afalg->opfd != -1) {
+ closesocket(afalg->opfd);
+ }
+
+ g_free(afalg);
+ }
+}
+
+QCryptoAfalg *afalg_comm_alloc(SocketAddress *saddr)
+{
+ QCryptoAfalg *afalg = NULL;
+ Error *err = NULL;
+
+ afalg = g_new0(QCryptoAfalg, 1);
+ /* initilize crypto API socket */
+ afalg->opfd = -1;
+ afalg->tfmfd = socket_bind(saddr, &err);
+ if (afalg->tfmfd == -1) {
+ goto error;
+ }
+
+ afalg->opfd = qemu_accept(afalg->tfmfd, NULL, 0);
+ if (afalg->opfd == -1) {
+ closesocket(afalg->tfmfd);
+ goto error;
+ }
+
+ return afalg;
+
+error:
+ error_free(err);
+ afalg_comm_free(afalg);
+ return NULL;
+}
diff --git a/include/crypto/afalg-comm.h b/include/crypto/afalg-comm.h
new file mode 100644
index 0000000..b6b9464
--- /dev/null
+++ b/include/crypto/afalg-comm.h
@@ -0,0 +1,61 @@
+/*
+ * QEMU Crypto af_alg support
+ *
+ * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
+ *
+ * Authors:
+ * Longpeng(Mike) <longpeng2@huawei.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version. See the COPYING file in the
+ * top-level directory.
+ */
+#ifndef QCRYPTO_AFALG_H
+#define QCRYPTO_AFALG_H
+
+#include "qapi-types.h"
+
+#ifndef SOL_ALG
+#define SOL_ALG 279
+#endif
+
+typedef struct QCryptoAfalg QCryptoAfalg;
+struct QCryptoAfalg {
+ int tfmfd;
+ int opfd;
+ struct msghdr *msg;
+ struct cmsghdr *cmsg;
+};
+
+
+/**
+ * afalg_comm_format_type:
+ * @afalg: the AfalgSocketAddress object
+ * @type: the type of crypto alg.
+ *
+ * Set the type field of the @afalg according to @type.
+ */
+void afalg_comm_format_type(AfalgSocketAddress *afalg,
+ const char *type);
+
+/**
+ * afalg_comm_alloc:
+ * @saddr: the SocketAddress object
+ *
+ * Allocate a QCryptoAfalg object and bind itself to
+ * a AF_ALG socket.
+ *
+ * Returns:
+ * a new QCryptoAfalg object, or NULL in error.
+ */
+QCryptoAfalg *afalg_comm_alloc(SocketAddress *saddr);
+
+/**
+ * afalg_comm_free:
+ * @afalg: the QCryptoAfalg object
+ *
+ * Free the @afalg.
+ */
+void afalg_comm_free(QCryptoAfalg *afalg);
+
+#endif
--
1.8.3.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH for-2.10 13/19] crypto: introduce some common functions for af_alg backend
2017-04-10 9:00 [Qemu-devel] [PATCH for-2.10 13/19] crypto: introduce some common functions for af_alg backend Longpeng(Mike)
@ 2017-04-10 10:15 ` Daniel P. Berrange
0 siblings, 0 replies; 2+ messages in thread
From: Daniel P. Berrange @ 2017-04-10 10:15 UTC (permalink / raw)
To: Longpeng(Mike); +Cc: xuquan8, arei.gonglei, qemu-devel
On Mon, Apr 10, 2017 at 05:00:47PM +0800, Longpeng(Mike) wrote:
> This patch introduces some common functions for af_alg backend,
> they would be used in af_alg-backend cipher/hash/hmac latter.
>
> Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
> ---
> crypto/Makefile.objs | 1 +
> crypto/afalg-comm.c | 71 +++++++++++++++++++++++++++++++++++++++++++++
> include/crypto/afalg-comm.h | 61 ++++++++++++++++++++++++++++++++++++++
> 3 files changed, 133 insertions(+)
> create mode 100644 crypto/afalg-comm.c
> create mode 100644 include/crypto/afalg-comm.h
>
> diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs
> index 1f749f2..6f244a3 100644
> --- a/crypto/Makefile.objs
> +++ b/crypto/Makefile.objs
> @@ -10,6 +10,7 @@ crypto-obj-$(if $(CONFIG_NETTLE),n,$(if $(CONFIG_GCRYPT_HMAC),n,y)) += hmac-glib
> crypto-obj-y += aes.o
> crypto-obj-y += desrfb.o
> crypto-obj-y += cipher.o
> +crypto-obj-$(CONFIG_AF_ALG) += afalg-comm.o
> crypto-obj-y += tlscreds.o
> crypto-obj-y += tlscredsanon.o
> crypto-obj-y += tlscredsx509.o
> diff --git a/crypto/afalg-comm.c b/crypto/afalg-comm.c
> new file mode 100644
> index 0000000..27bc88c
> --- /dev/null
> +++ b/crypto/afalg-comm.c
Just calls this crypto/afalg.c
> +void afalg_comm_format_type(AfalgSocketAddress *afalg,
> + const char *type)
> +{
> + afalg->type = (char *)g_new0(int8_t, SALG_TYPE_LEN_MAX);
> + pstrcpy(afalg->type, SALG_TYPE_LEN_MAX, type);
> +}
Needs an 'Error **errp' for reporting if 'type' is too long
> diff --git a/include/crypto/afalg-comm.h b/include/crypto/afalg-comm.h
> new file mode 100644
> index 0000000..b6b9464
> --- /dev/null
> +++ b/include/crypto/afalg-comm.h
> @@ -0,0 +1,61 @@
> +/*
> + * QEMU Crypto af_alg support
> + *
> + * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
> + *
> + * Authors:
> + * Longpeng(Mike) <longpeng2@huawei.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or
> + * (at your option) any later version. See the COPYING file in the
> + * top-level directory.
> + */
> +#ifndef QCRYPTO_AFALG_H
> +#define QCRYPTO_AFALG_H
> +
> +#include "qapi-types.h"
> +
> +#ifndef SOL_ALG
> +#define SOL_ALG 279
> +#endif
This can be in the .c file as I don't se it used elsewhere.
> +
> +typedef struct QCryptoAfalg QCryptoAfalg;
> +struct QCryptoAfalg {
Name it 'QCryptoAFAlg' since the letters 'af' are an abbreviation.
> + int tfmfd;
> + int opfd;
> + struct msghdr *msg;
> + struct cmsghdr *cmsg;
> +};
> +
> +
> +/**
> + * afalg_comm_format_type:
> + * @afalg: the AfalgSocketAddress object
> + * @type: the type of crypto alg.
> + *
> + * Set the type field of the @afalg according to @type.
> + */
> +void afalg_comm_format_type(AfalgSocketAddress *afalg,
> + const char *type);
> +
> +/**
> + * afalg_comm_alloc:
> + * @saddr: the SocketAddress object
> + *
> + * Allocate a QCryptoAfalg object and bind itself to
> + * a AF_ALG socket.
> + *
> + * Returns:
> + * a new QCryptoAfalg object, or NULL in error.
> + */
> +QCryptoAfalg *afalg_comm_alloc(SocketAddress *saddr);
> +
> +/**
> + * afalg_comm_free:
> + * @afalg: the QCryptoAfalg object
> + *
> + * Free the @afalg.
> + */
> +void afalg_comm_free(QCryptoAfalg *afalg);
> +
> +#endif
This is all for internal use by the crypto/ code, so should be in a header
crypto/afalgpriv.h
> --
> 1.8.3.1
>
>
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/ :|
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2017-04-10 10:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-10 9:00 [Qemu-devel] [PATCH for-2.10 13/19] crypto: introduce some common functions for af_alg backend Longpeng(Mike)
2017-04-10 10:15 ` Daniel P. Berrange
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).