2
0

wolfssl_example.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* wolfssl_example.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 "xil_printf.h"
  22. #include "xrtcpsu.h"
  23. #ifdef FREERTOS
  24. /* FreeRTOS includes. */
  25. #include "FreeRTOS.h"
  26. #include "task.h"
  27. #include "queue.h"
  28. #include "timers.h"
  29. #endif
  30. #include "wolfssl/wolfcrypt/settings.h"
  31. #include "wolfssl/wolfcrypt/wc_port.h"
  32. #include "wolfssl/wolfcrypt/error-crypt.h"
  33. #include "wolfssl/wolfcrypt/logging.h"
  34. #include "wolfcrypt/test/test.h"
  35. #include "wolfcrypt/benchmark/benchmark.h"
  36. /*****************************************************************************
  37. * Configuration
  38. ****************************************************************************/
  39. /*****************************************************************************
  40. * Private types/enumerations/variables
  41. ****************************************************************************/
  42. /*****************************************************************************
  43. * Public types/enumerations/variables
  44. ****************************************************************************/
  45. typedef struct func_args
  46. {
  47. int argc;
  48. char **argv;
  49. int return_code;
  50. } func_args;
  51. const char menu1[] = "\n"
  52. "\tb. WolfCrypt Benchmark\n"
  53. "\tt. WolfCrypt Test\n"
  54. "\n"
  55. "\tc. Command mode\n";
  56. /*****************************************************************************
  57. * Private functions
  58. ****************************************************************************/
  59. #if !defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  60. /* Test RNG Seed Function */
  61. /* TODO: Must provide real seed to RNG */
  62. unsigned char my_rng_seed_gen(void)
  63. {
  64. static unsigned int kTestSeed = 1;
  65. return kTestSeed++;
  66. }
  67. #endif
  68. /*****************************************************************************
  69. * Public functions
  70. ****************************************************************************/
  71. #ifdef FREERTOS
  72. static void wolfssl_task( void *pvParameters );
  73. static TaskHandle_t xWolfsslTask;
  74. int main( void )
  75. {
  76. xil_printf("\nStarting up FreeRTOS\n");
  77. xTaskCreate(wolfssl_task, /* The function that implements the task. */
  78. (const char*) "Tx", /* Text name for the task, provided to assist debugging only. */
  79. configMINIMAL_STACK_SIZE, /* The stack allocated to the task. */
  80. NULL, /* The task parameter is not used, so set to NULL. */
  81. tskIDLE_PRIORITY, /* The task runs at the idle priority. */
  82. &xWolfsslTask);
  83. /* Start the task. */
  84. vTaskStartScheduler();
  85. /* If all is well, the scheduler will now be running, and the following line
  86. will never be reached. If the following line does execute, then there was
  87. insufficient FreeRTOS heap memory available for the idle and/or timer tasks
  88. to be created. See the memory management section on the FreeRTOS web site
  89. for more details. */
  90. for (;;)
  91. ;
  92. }
  93. static void wolfssl_task( void *pvParameters )
  94. #else
  95. int main()
  96. #endif
  97. {
  98. uint8_t cmd[128], c;
  99. char *arg[sizeof(cmd)/2 + 1];
  100. size_t cmdlen, argnum, n;
  101. func_args args;
  102. int (*func)(int argc, char** argv);
  103. int command_mode = 0;
  104. #ifdef DEBUG_WOLFSSL
  105. wolfSSL_Debugging_ON();
  106. #endif
  107. /* initialize wolfSSL */
  108. wolfCrypt_Init();
  109. while (1) {
  110. memset(&args, 0, sizeof(args));
  111. args.return_code = NOT_COMPILED_IN; /* default */
  112. if (!command_mode) {
  113. xil_printf("\n\t\t\t\tMENU\n");
  114. xil_printf(menu1);
  115. xil_printf("Please select one of the above options:\n");
  116. xil_printf("Both 'b' and 't' allow arguments as if you'd call the regular shell version.\n");
  117. }
  118. cmdlen = 0;
  119. do {
  120. c = inbyte();
  121. cmd[cmdlen] = c;
  122. if (!command_mode) outbyte(c);
  123. cmdlen++;
  124. cmdlen %= sizeof(cmd);
  125. } while (c != '\n' && c != '\r');
  126. if (cmdlen > 2) {
  127. outbyte('\n');
  128. /* Poor man's argv parser */
  129. XMEMSET(arg, 0, sizeof(arg));
  130. if (cmd[1] == ' ') {
  131. if (cmd[0] == 'b') {
  132. arg[0] = "wolfcrypt_benchmark";
  133. func = wolfcrypt_benchmark_main;
  134. } else if (cmd[0] == 't') {
  135. arg[0] = "wolfcrypt_test";
  136. func = wolfcrypt_test_main;
  137. }
  138. if (arg[0] != NULL) {
  139. argnum = 1;
  140. for (n = 2; n < cmdlen; ++n) {
  141. switch (cmd[n]) {
  142. case ' ':
  143. case '\t':
  144. case '\r':
  145. case '\n':
  146. cmd[n] = '\0';
  147. if (arg[argnum] != NULL)
  148. argnum++;
  149. break;
  150. default:
  151. if (arg[argnum] == NULL)
  152. arg[argnum] = (char*)&cmd[n];
  153. break;
  154. }
  155. }
  156. func(argnum, arg);
  157. }
  158. }
  159. } else {
  160. switch (cmd[0]) {
  161. case 't':
  162. xil_printf("Running wolfCrypt Tests...\n");
  163. #ifndef NO_CRYPT_TEST
  164. args.return_code = 0;
  165. wolfcrypt_test(&args);
  166. #endif
  167. xil_printf("Crypt Test: Return code %d\n", args.return_code);
  168. break;
  169. case 'b':
  170. xil_printf("Running wolfCrypt Benchmarks...\n");
  171. #ifndef NO_CRYPT_BENCHMARK
  172. args.return_code = 0;
  173. benchmark_test(&args);
  174. #endif
  175. xil_printf("Benchmark Test: Return code %d\n", args.return_code);
  176. break;
  177. case 'c':
  178. if (!command_mode) xil_printf("Entering command mode, enter 'c<enter>' to return to manual mode\n");
  179. command_mode ^= 1;
  180. break;
  181. default:
  182. if (!command_mode) xil_printf("\nSelection out of range\n");
  183. break;
  184. }
  185. }
  186. }
  187. wolfCrypt_Cleanup();
  188. #ifndef FREERTOS
  189. return 0;
  190. #endif
  191. }