liboqs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/error-crypt.h>
  32. #include <wolfssl/wolfcrypt/port/liboqs/liboqs.h>
  33. #if defined(HAVE_LIBOQS)
  34. /* RNG for liboqs */
  35. static WC_RNG liboqsDefaultRNG;
  36. static WC_RNG* liboqsCurrentRNG;
  37. static wolfSSL_Mutex liboqsRNGMutex;
  38. static int liboqs_init = 0;
  39. static void wolfSSL_liboqsGetRandomData(uint8_t* buffer, size_t numOfBytes)
  40. {
  41. int ret = wc_RNG_GenerateBlock(liboqsCurrentRNG, buffer, numOfBytes);
  42. if (ret != 0) {
  43. // ToDo: liboqs exits programm if RNG fails, not sure what to do here
  44. }
  45. }
  46. int wolfSSL_liboqsInit(void)
  47. {
  48. int ret = 0;
  49. if (liboqs_init == 0) {
  50. ret = wc_InitMutex(&liboqsRNGMutex);
  51. if (ret != 0) {
  52. return ret;
  53. }
  54. ret = wc_LockMutex(&liboqsRNGMutex);
  55. if (ret != 0) {
  56. return ret;
  57. }
  58. ret = wc_InitRng(&liboqsDefaultRNG);
  59. if (ret == 0) {
  60. OQS_init();
  61. liboqs_init = 1;
  62. }
  63. liboqsCurrentRNG = &liboqsDefaultRNG;
  64. wc_UnLockMutex(&liboqsRNGMutex);
  65. OQS_randombytes_custom_algorithm(wolfSSL_liboqsGetRandomData);
  66. }
  67. return ret;
  68. }
  69. int wolfSSL_liboqsRngMutexLock(WC_RNG* rng)
  70. {
  71. int ret = wolfSSL_liboqsInit();
  72. if (ret == 0) {
  73. ret = wc_LockMutex(&liboqsRNGMutex);
  74. }
  75. if (ret == 0 && rng != NULL) {
  76. /* Update the pointer with the RNG to use. This is safe as we locked the mutex */
  77. liboqsCurrentRNG = rng;
  78. }
  79. return ret;
  80. }
  81. int wolfSSL_liboqsRngMutexUnlock(void)
  82. {
  83. int ret = BAD_MUTEX_E;
  84. liboqsCurrentRNG = &liboqsDefaultRNG;
  85. if (liboqs_init) {
  86. ret = wc_UnLockMutex(&liboqsRNGMutex);
  87. }
  88. return ret;
  89. }
  90. #endif /* HAVE_LIBOQS */