wolfssl_netos_custom.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* wolfssl_netos_custom.c
  2. *
  3. * Copyright (C) 2006-2024 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. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include <tx_api.h>
  24. #include <entropy.h>
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/fips_test.h>
  28. int dc_log_printf(char* format, ...);
  29. #if BSP_SIGMA == 0
  30. int dc_log_printf(char* format, ...)
  31. {
  32. va_list args;
  33. va_start(args, (format));
  34. fflush(stdout);
  35. vprintf(format, args);
  36. fflush(stdout);
  37. va_end(args);
  38. return 0;
  39. }
  40. #endif
  41. unsigned char get_byte_from_pool(void)
  42. {
  43. unsigned char out;
  44. float density;
  45. /* Wait until pool has at least one byte */
  46. /* TODO: improve this */
  47. while (ent_get_byte_count() == 0)
  48. tx_thread_sleep(1);
  49. /* Stop gathering entropy to avoid race conditions */
  50. ent_set_status(0);
  51. /* Pop a single byte from the pool and continue gathering entropy */
  52. ent_pop(&out, &density);
  53. ent_set_status(1);
  54. return out;
  55. }
  56. int my_rng_generate_seed(unsigned char* output, int sz)
  57. {
  58. word32 i;
  59. srand(get_byte_from_pool());
  60. for (i = 0; i < sz; i++) {
  61. output[i] = (unsigned char) rand();
  62. srand(get_byte_from_pool());
  63. }
  64. return 0;
  65. }
  66. static void appFipsCb(int ok, int err, const char* hash)
  67. {
  68. dc_log_printf("in appFipsCb Fips callback, ok = %d, err = %d\n", ok, err);
  69. dc_log_printf("message = %s\n", wc_GetErrorString(err));
  70. dc_log_printf("hash = %s\n", hash);
  71. if (err == IN_CORE_FIPS_E) {
  72. dc_log_printf("In core integrity hash check failure, copy above hash\n");
  73. dc_log_printf("into verifyCore[] in fips_test.c and rebuild\n");
  74. }
  75. }
  76. void setAppFipsCb(void)
  77. {
  78. wolfCrypt_SetCb_fips(appFipsCb);
  79. }