1
0

wolf_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* wolf_main.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. /* wolfSSL includes */
  22. #ifndef WOLFSSL_USER_SETTINGS
  23. #include <wolfssl/options.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/random.h> /* for CUSTOM_RAND_TYPE */
  28. #include <wolfcrypt/test/test.h>
  29. #include <wolfcrypt/benchmark/benchmark.h>
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <string.h>
  34. /* Infineon Includes */
  35. #include "Ifx_Types.h"
  36. #include "IfxStm.h"
  37. extern int send_UART(const char* str);
  38. static void my_logging_cb(const int logLevel, const char *const logMessage)
  39. {
  40. send_UART(logMessage);
  41. send_UART("\r\n");
  42. (void)logLevel; /* not used */
  43. }
  44. /* TIME CODE */
  45. /* Optionally you can define NO_ASN_TIME to disable all cert time checks */
  46. static int hw_get_time_sec(void)
  47. {
  48. /* get time in seconds */
  49. return IfxStm_get(&MODULE_STM0) / IfxStm_getFrequency(&MODULE_STM0);
  50. }
  51. /* This is used by wolfCrypt asn.c for cert time checking */
  52. unsigned long my_time(unsigned long* timer)
  53. {
  54. (void)timer;
  55. return hw_get_time_sec();
  56. }
  57. #ifndef WOLFCRYPT_ONLY
  58. /* This is used by TLS only */
  59. unsigned int LowResTimer(void)
  60. {
  61. return hw_get_time_sec();
  62. }
  63. #endif
  64. #ifndef NO_CRYPT_BENCHMARK
  65. /* This is used by wolfCrypt benchmark tool only */
  66. double current_time(int reset)
  67. {
  68. double timeNow;
  69. uint64_t timeMs, ticks = IfxStm_get(&MODULE_STM0);
  70. (void)reset;
  71. timeMs = ticks / (IfxStm_getFrequency(&MODULE_STM0) / 1000);
  72. timeNow = (timeMs / 1000); // sec
  73. timeNow += (double)(timeMs % 1000) / 1000; // ms
  74. return timeNow;
  75. }
  76. #endif
  77. /* RNG CODE */
  78. /* TODO: Implement real RNG */
  79. static unsigned int gCounter;
  80. unsigned int hw_rand(void)
  81. {
  82. //#warning Must implement your own random source
  83. return ++gCounter;
  84. }
  85. unsigned int my_rng_seed_gen(void)
  86. {
  87. return hw_rand();
  88. }
  89. typedef struct func_args {
  90. int argc;
  91. char** argv;
  92. int return_code;
  93. } func_args;
  94. void run_wolf_tests(void)
  95. {
  96. func_args args;
  97. #ifdef DEBUG_WOLFSSL
  98. wolfSSL_Debugging_ON();
  99. #endif
  100. wolfSSL_SetLoggingCb(my_logging_cb);
  101. /* initialize wolfSSL */
  102. #ifdef WOLFCRYPT_ONLY
  103. wolfCrypt_Init();
  104. #else
  105. wolfSSL_Init();
  106. #endif
  107. memset(&args, 0, sizeof(args));
  108. args.return_code = NOT_COMPILED_IN; /* default */
  109. printf("Running wolfCrypt Tests...\n");
  110. #ifndef NO_CRYPT_TEST
  111. args.return_code = 0;
  112. wolfcrypt_test(&args);
  113. printf("Crypt Test: Return code %d\n", args.return_code);
  114. #else
  115. args.return_code = NOT_COMPILED_IN;
  116. #endif
  117. printf("Running wolfCrypt Benchmarks...\n");
  118. #ifndef NO_CRYPT_BENCHMARK
  119. args.return_code = 0;
  120. benchmark_test(&args);
  121. #else
  122. args.return_code = NOT_COMPILED_IN;
  123. #endif
  124. printf("Benchmark Test: Return code %d\n", args.return_code);
  125. #ifdef WOLFCRYPT_ONLY
  126. wolfCrypt_Cleanup();
  127. #else
  128. wolfSSL_Cleanup();
  129. #endif
  130. }