blob: fb7f1bff1053526a8b7c1f097ada5951f907ab8a [file] [log] [blame]
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +01001/*
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
Thomas Huthb7cbb872019-02-13 16:54:59 +01009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010010 *
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
Peter Maydell42f7a442016-01-26 18:16:55 +000021#include "qemu/osdep.h"
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010022#include "crypto/init.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Daniel P. Berrange62893b62015-07-01 18:10:33 +010024#include "qemu/thread.h"
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010025
26#ifdef CONFIG_GNUTLS
27#include <gnutls/gnutls.h>
28#include <gnutls/crypto.h>
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +010029#endif
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010030
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +010031#ifdef CONFIG_GCRYPT
Daniel P. Berrange62893b62015-07-01 18:10:33 +010032#include <gcrypt.h>
33#endif
34
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020035#include "crypto/random.h"
36
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010037/* #define DEBUG_GNUTLS */
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010038#ifdef DEBUG_GNUTLS
39static void qcrypto_gnutls_log(int level, const char *str)
40{
41 fprintf(stderr, "%d: %s", level, str);
42}
43#endif
44
45int qcrypto_init(Error **errp)
46{
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +010047#ifdef CONFIG_GNUTLS
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010048 int ret;
49 ret = gnutls_global_init();
50 if (ret < 0) {
51 error_setg(errp,
52 "Unable to initialize GNUTLS library: %s",
53 gnutls_strerror(ret));
54 return -1;
55 }
56#ifdef DEBUG_GNUTLS
57 gnutls_global_set_log_level(10);
58 gnutls_global_set_log_function(qcrypto_gnutls_log);
59#endif
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +010060#endif
Daniel P. Berrange62893b62015-07-01 18:10:33 +010061
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +010062#ifdef CONFIG_GCRYPT
Richard W.M. Jonesd6cca8e2020-05-27 10:34:09 +010063 if (!gcry_check_version(NULL)) {
Daniel P. Berrange62893b62015-07-01 18:10:33 +010064 error_setg(errp, "Unable to initialize gcrypt");
65 return -1;
66 }
Daniel P. Berrange62893b62015-07-01 18:10:33 +010067 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
68#endif
69
Geert Martin Ijewskia3727812017-04-26 00:15:01 +020070 if (qcrypto_random_init(errp) < 0) {
71 return -1;
72 }
73
Daniel P. Berrangeddbb0d02015-07-01 18:10:29 +010074 return 0;
75}