]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/init.c
crypto: require gnutls >= 3.1.18 for building QEMU
[mirror_qemu.git] / crypto / init.c
CommitLineData
ddbb0d09
DB
1/*
2 * QEMU Crypto initialization
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
42f7a448 21#include "qemu/osdep.h"
ddbb0d09 22#include "crypto/init.h"
da34e65c 23#include "qapi/error.h"
62893b67 24#include "qemu/thread.h"
ddbb0d09
DB
25
26#ifdef CONFIG_GNUTLS
27#include <gnutls/gnutls.h>
28#include <gnutls/crypto.h>
91bfcdb0 29#endif
ddbb0d09 30
91bfcdb0 31#ifdef CONFIG_GCRYPT
62893b67
DB
32#include <gcrypt.h>
33#endif
34
a3727816
GMI
35#include "crypto/random.h"
36
ddbb0d09
DB
37/* #define DEBUG_GNUTLS */
38
62893b67 39/*
a0722409 40 * We need to init gcrypt threading if
62893b67
DB
41 *
42 * - gcrypt < 1.6.0
62893b67
DB
43 *
44 */
45
91bfcdb0 46#if (defined(CONFIG_GCRYPT) && \
62893b67
DB
47 (!defined(GCRYPT_VERSION_NUMBER) || \
48 (GCRYPT_VERSION_NUMBER < 0x010600)))
49#define QCRYPTO_INIT_GCRYPT_THREADS
50#else
51#undef QCRYPTO_INIT_GCRYPT_THREADS
52#endif
53
ddbb0d09
DB
54#ifdef DEBUG_GNUTLS
55static void qcrypto_gnutls_log(int level, const char *str)
56{
57 fprintf(stderr, "%d: %s", level, str);
58}
59#endif
60
62893b67
DB
61#ifdef QCRYPTO_INIT_GCRYPT_THREADS
62static int qcrypto_gcrypt_mutex_init(void **priv)
63{ \
64 QemuMutex *lock = NULL;
65 lock = g_new0(QemuMutex, 1);
66 qemu_mutex_init(lock);
67 *priv = lock;
68 return 0;
69}
70
71static int qcrypto_gcrypt_mutex_destroy(void **priv)
72{
73 QemuMutex *lock = *priv;
74 qemu_mutex_destroy(lock);
75 g_free(lock);
76 return 0;
77}
78
79static int qcrypto_gcrypt_mutex_lock(void **priv)
80{
81 QemuMutex *lock = *priv;
82 qemu_mutex_lock(lock);
83 return 0;
84}
85
86static int qcrypto_gcrypt_mutex_unlock(void **priv)
87{
88 QemuMutex *lock = *priv;
89 qemu_mutex_unlock(lock);
90 return 0;
91}
92
93static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = {
94 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
95 NULL,
96 qcrypto_gcrypt_mutex_init,
97 qcrypto_gcrypt_mutex_destroy,
98 qcrypto_gcrypt_mutex_lock,
99 qcrypto_gcrypt_mutex_unlock,
100 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
101};
102#endif /* QCRYPTO_INIT_GCRYPT */
103
ddbb0d09
DB
104int qcrypto_init(Error **errp)
105{
37316663
DB
106#ifdef QCRYPTO_INIT_GCRYPT_THREADS
107 gcry_control(GCRYCTL_SET_THREAD_CBS, &qcrypto_gcrypt_thread_impl);
108#endif /* QCRYPTO_INIT_GCRYPT_THREADS */
109
91bfcdb0 110#ifdef CONFIG_GNUTLS
ddbb0d09
DB
111 int ret;
112 ret = gnutls_global_init();
113 if (ret < 0) {
114 error_setg(errp,
115 "Unable to initialize GNUTLS library: %s",
116 gnutls_strerror(ret));
117 return -1;
118 }
119#ifdef DEBUG_GNUTLS
120 gnutls_global_set_log_level(10);
121 gnutls_global_set_log_function(qcrypto_gnutls_log);
122#endif
91bfcdb0 123#endif
62893b67 124
91bfcdb0 125#ifdef CONFIG_GCRYPT
62893b67
DB
126 if (!gcry_check_version(GCRYPT_VERSION)) {
127 error_setg(errp, "Unable to initialize gcrypt");
128 return -1;
129 }
62893b67
DB
130 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
131#endif
132
a3727816
GMI
133 if (qcrypto_random_init(errp) < 0) {
134 return -1;
135 }
136
ddbb0d09
DB
137 return 0;
138}