crypto: add a gcrypt cipher implementation
[qemu.git] / crypto / init.c
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
21 #include "crypto/init.h"
22 #include "qemu/thread.h"
23
24 #ifdef CONFIG_GNUTLS
25 #include <gnutls/gnutls.h>
26 #include <gnutls/crypto.h>
27
28 #ifdef CONFIG_GNUTLS_GCRYPT
29 #include <gcrypt.h>
30 #endif
31
32 /* #define DEBUG_GNUTLS */
33
34 /*
35 * If GNUTLS is built against GCrypt then
36 *
37 * - When GNUTLS >= 2.12, we must not initialize gcrypt threading
38 * because GNUTLS will do that itself
39 * - When GNUTLS < 2.12 we must always initialize gcrypt threading
40 *
41 * But....
42 *
43 * When gcrypt >= 1.6.0 we must not initialize gcrypt threading
44 * because gcrypt will do that itself.
45 *
46 * So we need to init gcrypt threading if
47 *
48 * - gcrypt < 1.6.0
49 * AND
50 * - gnutls < 2.12
51 *
52 */
53
54 #if (defined(CONFIG_GNUTLS_GCRYPT) && \
55 (!defined(GNUTLS_VERSION_NUMBER) || \
56 (GNUTLS_VERSION_NUMBER < 0x020c00)) && \
57 (!defined(GCRYPT_VERSION_NUMBER) || \
58 (GCRYPT_VERSION_NUMBER < 0x010600)))
59 #define QCRYPTO_INIT_GCRYPT_THREADS
60 #else
61 #undef QCRYPTO_INIT_GCRYPT_THREADS
62 #endif
63
64 #ifdef DEBUG_GNUTLS
65 static void qcrypto_gnutls_log(int level, const char *str)
66 {
67 fprintf(stderr, "%d: %s", level, str);
68 }
69 #endif
70
71 #ifdef QCRYPTO_INIT_GCRYPT_THREADS
72 static int qcrypto_gcrypt_mutex_init(void **priv)
73 { \
74 QemuMutex *lock = NULL;
75 lock = g_new0(QemuMutex, 1);
76 qemu_mutex_init(lock);
77 *priv = lock;
78 return 0;
79 }
80
81 static int qcrypto_gcrypt_mutex_destroy(void **priv)
82 {
83 QemuMutex *lock = *priv;
84 qemu_mutex_destroy(lock);
85 g_free(lock);
86 return 0;
87 }
88
89 static int qcrypto_gcrypt_mutex_lock(void **priv)
90 {
91 QemuMutex *lock = *priv;
92 qemu_mutex_lock(lock);
93 return 0;
94 }
95
96 static int qcrypto_gcrypt_mutex_unlock(void **priv)
97 {
98 QemuMutex *lock = *priv;
99 qemu_mutex_unlock(lock);
100 return 0;
101 }
102
103 static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = {
104 (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
105 NULL,
106 qcrypto_gcrypt_mutex_init,
107 qcrypto_gcrypt_mutex_destroy,
108 qcrypto_gcrypt_mutex_lock,
109 qcrypto_gcrypt_mutex_unlock,
110 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
111 };
112 #endif /* QCRYPTO_INIT_GCRYPT */
113
114 int qcrypto_init(Error **errp)
115 {
116 int ret;
117 ret = gnutls_global_init();
118 if (ret < 0) {
119 error_setg(errp,
120 "Unable to initialize GNUTLS library: %s",
121 gnutls_strerror(ret));
122 return -1;
123 }
124 #ifdef DEBUG_GNUTLS
125 gnutls_global_set_log_level(10);
126 gnutls_global_set_log_function(qcrypto_gnutls_log);
127 #endif
128
129 #ifdef CONFIG_GNUTLS_GCRYPT
130 if (!gcry_check_version(GCRYPT_VERSION)) {
131 error_setg(errp, "Unable to initialize gcrypt");
132 return -1;
133 }
134 #ifdef QCRYPTO_INIT_GCRYPT_THREADS
135 gcry_control(GCRYCTL_SET_THREAD_CBS, &qcrypto_gcrypt_thread_impl);
136 #endif /* QCRYPTO_INIT_GCRYPT_THREADS */
137 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
138 #endif
139
140 return 0;
141 }
142
143 #else /* ! CONFIG_GNUTLS */
144
145 int qcrypto_init(Error **errp G_GNUC_UNUSED)
146 {
147 return 0;
148 }
149
150 #endif /* ! CONFIG_GNUTLS */