wolf_main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "user_settings.h"
  22. #include "wolfssl/ssl.h"
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include "wolf_demo.h"
  26. static WOLFSSL_CTX *wolfSSL_sv_ctx;
  27. static WOLFSSL_CTX *wolfSSL_cl_ctx;
  28. static long tick;
  29. static void timeTick(void *pdata)
  30. {
  31. tick++;
  32. }
  33. #define FREQ 10000 /* Hz */
  34. double current_time(int reset)
  35. {
  36. if(reset) tick = 0 ;
  37. return ((double)tick/FREQ) ;
  38. }
  39. #define ARG_SZ 256
  40. static char argBuff[ARG_SZ];
  41. static int get_arg(func_args *args)
  42. {
  43. int i;
  44. char *arg = argBuff;
  45. args->argc = 0;
  46. for(i=0; i<ARG_SZ; i++) {
  47. *arg = getchar();
  48. switch(*arg){
  49. case '\n':
  50. *arg = '\0';
  51. return args->argc;
  52. case ' ':
  53. *arg++ = '\0';
  54. while(*arg++ == ' '); /* Skip space */
  55. args->argv[++args->argc] = arg;
  56. break;
  57. default:
  58. arg++;
  59. }
  60. }
  61. return args->argc ;
  62. }
  63. void wolfSSL_init()
  64. {
  65. uint32_t channel;
  66. R_CMT_CreatePeriodic(FREQ, &timeTick, &channel);
  67. wolfSSL_sv_ctx = wolfSSL_TLS_server_init();
  68. wolfSSL_cl_ctx = wolfSSL_TLS_client_init();
  69. wolfSSL_main();
  70. }
  71. void wolfSSL_main()
  72. {
  73. int c;
  74. func_args args = {0};
  75. printf("wolfSSL Demo\nt: test, b: benchmark, s: server, or c <IP addr> <Port>: client\n$ ");
  76. c = getchar();
  77. switch(c) {
  78. case 't':
  79. get_arg(&args);
  80. printf("Start wolfCrypt Test\n");
  81. wolfcrypt_test(&args);
  82. printf("End wolfCrypt Test\n");
  83. break;
  84. case 'b':
  85. get_arg(&args);
  86. printf("Start wolfCrypt Benchmark\n");
  87. benchmark_test(NULL);
  88. printf("End wolfCrypt Benchmark\n");
  89. break;
  90. case 'c':
  91. if(get_arg(&args) < 0)
  92. break;
  93. printf("Start TLS Client(%s, %s)\n", args.argv[1], args.argv[2]);
  94. wolfSSL_TLS_client(wolfSSL_cl_ctx, &args);
  95. printf("End TLS Client\n");
  96. break;
  97. case 's':
  98. if(get_arg(&args) < 0)
  99. break;
  100. printf("Start TLS Server\n");
  101. wolfSSL_TLS_server(wolfSSL_sv_ctx, &args);
  102. printf("End TLS Server\n");
  103. break;
  104. default:
  105. printf("Command Error\n");
  106. }
  107. }