xref: /openbmc/qemu/crypto/init.c (revision 7d87775f)
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.1 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 "qemu/osdep.h"
22 #include "crypto/init.h"
23 #include "qapi/error.h"
24 #include "qemu/thread.h"
25 
26 #ifdef CONFIG_GNUTLS
27 #include <gnutls/gnutls.h>
28 #include <gnutls/crypto.h>
29 #endif
30 
31 #ifdef CONFIG_GCRYPT
32 #include <gcrypt.h>
33 #endif
34 
35 #include "crypto/random.h"
36 
37 
38 /*
39  * To debug GNUTLS see env vars listed in
40  * https://gnutls.org/manual/html_node/Debugging-and-auditing.html
41  */
42 int qcrypto_init(Error **errp)
43 {
44 #ifdef CONFIG_GNUTLS
45     int ret;
46     ret = gnutls_global_init();
47     if (ret < 0) {
48         error_setg(errp,
49                    "Unable to initialize GNUTLS library: %s",
50                    gnutls_strerror(ret));
51         return -1;
52     }
53 #endif
54 
55 #ifdef CONFIG_GCRYPT
56     if (!gcry_check_version(NULL)) {
57         error_setg(errp, "Unable to initialize gcrypt");
58         return -1;
59     }
60     gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
61 #endif
62 
63     if (qcrypto_random_init(errp) < 0) {
64         return -1;
65     }
66 
67     return 0;
68 }
69