2 * QEMU Crypto af_alg support
4 * Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
7 * Longpeng(Mike) <longpeng2@huawei.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/cutils.h"
15 #include "qemu/sockets.h"
16 #include "qapi/error.h"
17 #include "afalgpriv.h"
20 qcrypto_afalg_build_saddr(const char *type
, const char *name
,
21 struct sockaddr_alg
*salg
, Error
**errp
)
23 salg
->salg_family
= AF_ALG
;
25 if (strnlen(type
, SALG_TYPE_LEN_MAX
) >= SALG_TYPE_LEN_MAX
) {
26 error_setg(errp
, "Afalg type(%s) is larger than %d bytes",
27 type
, SALG_TYPE_LEN_MAX
);
31 if (strnlen(name
, SALG_NAME_LEN_MAX
) >= SALG_NAME_LEN_MAX
) {
32 error_setg(errp
, "Afalg name(%s) is larger than %d bytes",
33 name
, SALG_NAME_LEN_MAX
);
37 pstrcpy((char *)salg
->salg_type
, SALG_TYPE_LEN_MAX
, type
);
38 pstrcpy((char *)salg
->salg_name
, SALG_NAME_LEN_MAX
, name
);
44 qcrypto_afalg_socket_bind(const char *type
, const char *name
,
48 struct sockaddr_alg salg
= {0};
50 if (!qcrypto_afalg_build_saddr(type
, name
, &salg
, errp
)) {
54 sbind
= qemu_socket(AF_ALG
, SOCK_SEQPACKET
, 0);
56 error_setg_errno(errp
, errno
, "Failed to create socket");
60 if (bind(sbind
, (const struct sockaddr
*)&salg
, sizeof(salg
)) != 0) {
61 error_setg_errno(errp
, errno
, "Failed to bind socket");
70 qcrypto_afalg_comm_alloc(const char *type
, const char *name
,
75 afalg
= g_new0(QCryptoAFAlg
, 1);
76 /* initilize crypto API socket */
78 afalg
->tfmfd
= qcrypto_afalg_socket_bind(type
, name
, errp
);
79 if (afalg
->tfmfd
== -1) {
83 afalg
->opfd
= qemu_accept(afalg
->tfmfd
, NULL
, 0);
84 if (afalg
->opfd
== -1) {
85 error_setg_errno(errp
, errno
, "Failed to accept socket");
92 qcrypto_afalg_comm_free(afalg
);
96 void qcrypto_afalg_comm_free(QCryptoAFAlg
*afalg
)
103 g_free(afalg
->msg
->msg_control
);
107 if (afalg
->tfmfd
!= -1) {
111 if (afalg
->opfd
!= -1) {