init.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /*
  34. * We need to init gcrypt threading if
  35. *
  36. * - gcrypt < 1.6.0
  37. *
  38. */
  39. #if (defined(CONFIG_GCRYPT) && \
  40. (GCRYPT_VERSION_NUMBER < 0x010600))
  41. #define QCRYPTO_INIT_GCRYPT_THREADS
  42. #else
  43. #undef QCRYPTO_INIT_GCRYPT_THREADS
  44. #endif
  45. #ifdef DEBUG_GNUTLS
  46. static void qcrypto_gnutls_log(int level, const char *str)
  47. {
  48. fprintf(stderr, "%d: %s", level, str);
  49. }
  50. #endif
  51. #ifdef QCRYPTO_INIT_GCRYPT_THREADS
  52. static int qcrypto_gcrypt_mutex_init(void **priv)
  53. { \
  54. QemuMutex *lock = NULL;
  55. lock = g_new0(QemuMutex, 1);
  56. qemu_mutex_init(lock);
  57. *priv = lock;
  58. return 0;
  59. }
  60. static int qcrypto_gcrypt_mutex_destroy(void **priv)
  61. {
  62. QemuMutex *lock = *priv;
  63. qemu_mutex_destroy(lock);
  64. g_free(lock);
  65. return 0;
  66. }
  67. static int qcrypto_gcrypt_mutex_lock(void **priv)
  68. {
  69. QemuMutex *lock = *priv;
  70. qemu_mutex_lock(lock);
  71. return 0;
  72. }
  73. static int qcrypto_gcrypt_mutex_unlock(void **priv)
  74. {
  75. QemuMutex *lock = *priv;
  76. qemu_mutex_unlock(lock);
  77. return 0;
  78. }
  79. static struct gcry_thread_cbs qcrypto_gcrypt_thread_impl = {
  80. (GCRY_THREAD_OPTION_PTHREAD | (GCRY_THREAD_OPTION_VERSION << 8)),
  81. NULL,
  82. qcrypto_gcrypt_mutex_init,
  83. qcrypto_gcrypt_mutex_destroy,
  84. qcrypto_gcrypt_mutex_lock,
  85. qcrypto_gcrypt_mutex_unlock,
  86. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  87. };
  88. #endif /* QCRYPTO_INIT_GCRYPT */
  89. int qcrypto_init(Error **errp)
  90. {
  91. #ifdef QCRYPTO_INIT_GCRYPT_THREADS
  92. gcry_control(GCRYCTL_SET_THREAD_CBS, &qcrypto_gcrypt_thread_impl);
  93. #endif /* QCRYPTO_INIT_GCRYPT_THREADS */
  94. #ifdef CONFIG_GNUTLS
  95. int ret;
  96. ret = gnutls_global_init();
  97. if (ret < 0) {
  98. error_setg(errp,
  99. "Unable to initialize GNUTLS library: %s",
  100. gnutls_strerror(ret));
  101. return -1;
  102. }
  103. #ifdef DEBUG_GNUTLS
  104. gnutls_global_set_log_level(10);
  105. gnutls_global_set_log_function(qcrypto_gnutls_log);
  106. #endif
  107. #endif
  108. #ifdef CONFIG_GCRYPT
  109. if (!gcry_check_version(GCRYPT_VERSION)) {
  110. error_setg(errp, "Unable to initialize gcrypt");
  111. return -1;
  112. }
  113. gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
  114. #endif
  115. if (qcrypto_random_init(errp) < 0) {
  116. return -1;
  117. }
  118. return 0;
  119. }