liboqs.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* liboqs.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library provides the support interfaces to the liboqs library providing
  24. implementations for Post-Quantum cryptography algorithms.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfssl/wolfcrypt/logging.h>
  32. #include <wolfssl/wolfcrypt/error-crypt.h>
  33. #include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
  34. #if defined(HAVE_LIBOQS)
  35. /* RNG for liboqs */
  36. static WC_RNG liboqsDefaultRNG;
  37. static WC_RNG* liboqsCurrentRNG;
  38. static wolfSSL_Mutex liboqsRNGMutex;
  39. static int liboqs_init = 0;
  40. static void wolfSSL_liboqsGetRandomData(uint8_t* buffer, size_t numOfBytes)
  41. {
  42. int ret;
  43. word32 numOfBytes_word32;
  44. while (numOfBytes > 0) {
  45. numOfBytes_word32 = (word32)numOfBytes;
  46. numOfBytes -= numOfBytes_word32;
  47. ret = wc_RNG_GenerateBlock(liboqsCurrentRNG, buffer,
  48. numOfBytes_word32);
  49. if (ret != 0) {
  50. /* ToDo: liboqs exits programm if RNG fails,
  51. * not sure what to do here
  52. */
  53. WOLFSSL_MSG_EX(
  54. "wc_RNG_GenerateBlock(..., %u) failed with ret %d "
  55. "in wolfSSL_liboqsGetRandomData().", numOfBytes_word32, ret
  56. );
  57. abort();
  58. }
  59. }
  60. }
  61. int wolfSSL_liboqsInit(void)
  62. {
  63. int ret = 0;
  64. if (liboqs_init == 0) {
  65. ret = wc_InitMutex(&liboqsRNGMutex);
  66. if (ret != 0) {
  67. return ret;
  68. }
  69. ret = wc_LockMutex(&liboqsRNGMutex);
  70. if (ret != 0) {
  71. return ret;
  72. }
  73. ret = wc_InitRng(&liboqsDefaultRNG);
  74. if (ret == 0) {
  75. OQS_init();
  76. liboqs_init = 1;
  77. }
  78. liboqsCurrentRNG = &liboqsDefaultRNG;
  79. wc_UnLockMutex(&liboqsRNGMutex);
  80. OQS_randombytes_custom_algorithm(wolfSSL_liboqsGetRandomData);
  81. }
  82. return ret;
  83. }
  84. void wolfSSL_liboqsClose(void)
  85. {
  86. wc_FreeRng(&liboqsDefaultRNG);
  87. }
  88. int wolfSSL_liboqsRngMutexLock(WC_RNG* rng)
  89. {
  90. int ret = wolfSSL_liboqsInit();
  91. if (ret == 0) {
  92. ret = wc_LockMutex(&liboqsRNGMutex);
  93. }
  94. if (ret == 0 && rng != NULL) {
  95. /* Update the pointer with the RNG to use. This is safe as we locked the mutex */
  96. liboqsCurrentRNG = rng;
  97. }
  98. return ret;
  99. }
  100. int wolfSSL_liboqsRngMutexUnlock(void)
  101. {
  102. int ret = BAD_MUTEX_E;
  103. liboqsCurrentRNG = &liboqsDefaultRNG;
  104. if (liboqs_init) {
  105. ret = wc_UnLockMutex(&liboqsRNGMutex);
  106. }
  107. return ret;
  108. }
  109. #endif /* HAVE_LIBOQS */