2
0

init.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "qemu/osdep.h"
  21. #include "crypto/init.h"
  22. #include "qapi/error.h"
  23. #include "qemu/thread.h"
  24. #ifdef CONFIG_GNUTLS
  25. #include <gnutls/gnutls.h>
  26. #include <gnutls/crypto.h>
  27. #endif
  28. #ifdef CONFIG_GCRYPT
  29. #include <gcrypt.h>
  30. #endif
  31. #include "crypto/random.h"
  32. /* #define DEBUG_GNUTLS */
  33. #ifdef DEBUG_GNUTLS
  34. static void qcrypto_gnutls_log(int level, const char *str)
  35. {
  36. fprintf(stderr, "%d: %s", level, str);
  37. }
  38. #endif
  39. int qcrypto_init(Error **errp)
  40. {
  41. #ifdef CONFIG_GNUTLS
  42. int ret;
  43. ret = gnutls_global_init();
  44. if (ret < 0) {
  45. error_setg(errp,
  46. "Unable to initialize GNUTLS library: %s",
  47. gnutls_strerror(ret));
  48. return -1;
  49. }
  50. #ifdef DEBUG_GNUTLS
  51. gnutls_global_set_log_level(10);
  52. gnutls_global_set_log_function(qcrypto_gnutls_log);
  53. #endif
  54. #endif
  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. if (qcrypto_random_init(errp) < 0) {
  63. return -1;
  64. }
  65. return 0;
  66. }