test_main.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* test_main.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 "stdint.h"
  23. #include <wolfssl/wolfcrypt/settings.h>
  24. #include "wolfssl/wolfcrypt/types.h"
  25. #include "wolfssl_demo.h"
  26. void main(void);
  27. #ifdef __cplusplus
  28. extern "C" {
  29. }
  30. #endif
  31. #if defined(TLS_CLIENT) || defined(TLS_SERVER)
  32. #include "r_tsip_rx_if.h"
  33. #include "r_t4_itcpip.h"
  34. #include "r_sys_time_rx_if.h"
  35. #include "Pin.h"
  36. #define T4_WORK_SIZE (14800)
  37. static UW tcpudp_work[(T4_WORK_SIZE / 4) + 1];
  38. #endif
  39. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  40. #include "key_data.h"
  41. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  42. extern const st_key_block_data_t g_key_block_data;
  43. user_PKCbInfo guser_PKCbInfo;
  44. #endif
  45. #if defined(TLS_CLIENT)
  46. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && defined(WOLFSSL_STATIC_MEMORY)
  47. #include <wolfssl/wolfcrypt/memory.h>
  48. WOLFSSL_HEAP_HINT* heapHint = NULL;
  49. #define BUFFSIZE_GEN (110 * 1024)
  50. unsigned char heapBufGen[BUFFSIZE_GEN];
  51. #endif /* WOLFSSL_RENESAS_TSIP_TLS && WOLFSSL_STATIC_MEMORY */
  52. #endif /* TLS_CLIENT */
  53. static long tick;
  54. static void timeTick(void *pdata)
  55. {
  56. tick++;
  57. }
  58. typedef struct func_args {
  59. int argc;
  60. char** argv;
  61. int return_code;
  62. } func_args;
  63. void wolfcrypt_test(func_args args);
  64. int benchmark_test(void *args);
  65. double current_time(int reset)
  66. {
  67. if(reset) tick = 0 ;
  68. return ((double)tick/FREQ) ;
  69. }
  70. #if defined(TLS_CLIENT) || defined(TLS_SERVER)
  71. int SetTsiptlsKey()
  72. {
  73. #if defined(WOLFSSL_RENESAS_TSIP) && (WOLFSSL_RENESAS_TSIP_VER >=109)
  74. #if defined(TLS_CLIENT)
  75. #if defined(USE_ECC_CERT)
  76. /* Root CA cert has ECC-P256 public key */
  77. tsip_inform_cert_sign((const byte *)ca_ecc_cert_der_sig);
  78. #else
  79. /* Root CA cert has RSA public key */
  80. tsip_inform_cert_sign((const byte *)ca_cert_der_sig);
  81. #endif
  82. tsip_inform_user_keys_ex(
  83. (byte*)&g_key_block_data.encrypted_provisioning_key,
  84. (byte*)&g_key_block_data.iv,
  85. (byte*)&g_key_block_data.encrypted_user_rsa2048_ne_key,
  86. encrypted_user_key_type);
  87. #elif defined(TLS_SERVER)
  88. tsip_inform_cert_sign((const byte *)client_cert_der_sign);
  89. tsip_inform_user_keys_ex(
  90. (byte *)&g_key_block_data.encrypted_provisioning_key,
  91. (byte *)&g_key_block_data.iv,
  92. (byte *)&g_key_block_data.encrypted_user_rsa2048_ne_key,
  93. encrypted_user_key_type);
  94. #endif
  95. #elif defined(WOLFSSL_RENESAS_TSIP) && (WOLFSSL_RENESAS_TSIP_VER < 109)
  96. #if defined(TLS_CLIENT)
  97. tsip_inform_cert_sign((const byte *)ca_cert_sig);
  98. tsip_inform_user_keys((byte*)&g_key_block_data.encrypted_session_key,
  99. (byte*)&g_key_block_data.iv,
  100. (byte*)&g_key_block_data.encrypted_user_rsa2048_ne_key);
  101. #elif defined(TLS_SERVER)
  102. tsip_inform_cert_sign((const byte *)client_cert_der_sign);
  103. tsip_inform_user_keys((byte*)&g_key_block_data.encrypted_session_key,
  104. (byte*)&g_key_block_data.iv,
  105. (byte*)&g_key_block_data.encrypted_user_rsa2048_ne_key);
  106. #endif
  107. #endif
  108. return 0;
  109. }
  110. int Open_tcp( )
  111. {
  112. ER ercd;
  113. W size;
  114. sys_time_err_t sys_ercd;
  115. char ver[128];
  116. /* initialize TSIP since t4 seems to call R_TSIP_RandomNumber */
  117. R_TSIP_Open(NULL,NULL);
  118. /* cast from uint8_t to char* */
  119. strcpy(ver, (char*)R_t4_version.library);
  120. sys_ercd = R_SYS_TIME_Open();
  121. if (sys_ercd != SYS_TIME_SUCCESS) {
  122. printf("ERROR : R_SYS_TIME_Open() failed\n");
  123. return -1;
  124. }
  125. R_Pins_Create();
  126. /* start LAN controller */
  127. ercd = lan_open();
  128. /* initialize TCP/IP */
  129. size = tcpudp_get_ramsize();
  130. if (size > (sizeof(tcpudp_work))) {
  131. printf("size > (sizeof(tcpudp_work))!\n");
  132. return -1;
  133. }
  134. ercd = tcpudp_open(tcpudp_work);
  135. if (ercd != E_OK) {
  136. printf("ERROR : tcpudp_open failed\n");
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. void Close_tcp()
  142. {
  143. /* end TCP/IP */
  144. tcpudp_close();
  145. lan_close();
  146. R_SYS_TIME_Close();
  147. R_TSIP_Close();
  148. }
  149. #endif
  150. void main(void)
  151. {
  152. int i = 0;
  153. int ret;
  154. int doClientCheck = 0;
  155. uint32_t channel;
  156. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  157. defined(TLS_CLIENT)
  158. #ifdef USE_ECC_CERT
  159. const char* cipherlist[] = {
  160. #if defined(WOLFSSL_TLS13)
  161. "TLS13-AES128-GCM-SHA256",
  162. "TLS13-AES128-CCM-SHA256",
  163. #endif
  164. "ECDHE-ECDSA-AES128-GCM-SHA256",
  165. "ECDHE-ECDSA-AES128-SHA256"
  166. };
  167. int cipherlist_sz;
  168. #if defined(WOLFSSL_TLS13)
  169. cipherlist_sz = 2;
  170. #else
  171. cipherlist_sz = 2;
  172. #endif
  173. #else
  174. const char* cipherlist[] = {
  175. #if defined(WOLFSSL_TLS13)
  176. "TLS13-AES128-GCM-SHA256",
  177. "TLS13-AES128-CCM-SHA256",
  178. #endif
  179. "ECDHE-RSA-AES128-GCM-SHA256",
  180. "ECDHE-RSA-AES128-SHA256",
  181. "AES128-SHA",
  182. "AES128-SHA256",
  183. "AES256-SHA",
  184. "AES256-SHA256"
  185. };
  186. int cipherlist_sz;
  187. #if defined(WOLFSSL_TLS13)
  188. cipherlist_sz = 2;
  189. #else
  190. cipherlist_sz = 6;
  191. #endif /* WOLFSSL_TLS13 */
  192. #endif
  193. #endif
  194. (void)timeTick;
  195. (void)i;
  196. (void)ret;
  197. (void)channel;
  198. (void)doClientCheck;
  199. #if defined(CRYPT_TEST) || defined(BENCHMARK)
  200. #if defined(CRYPT_TEST)
  201. func_args args = { 0 };
  202. if ((ret = wolfCrypt_Init()) != 0) {
  203. printf("wolfCrypt_Init failed %d\n", ret);
  204. }
  205. printf("Start wolfCrypt Test\n");
  206. wolfcrypt_test(args);
  207. printf("End wolfCrypt Test\n");
  208. if ((ret = wolfCrypt_Cleanup()) != 0) {
  209. printf("wolfCrypt_Cleanup failed %d\n", ret);
  210. }
  211. #endif
  212. #if defined(BENCHMARK)
  213. #include "r_cmt_rx_if.h"
  214. R_CMT_CreatePeriodic(FREQ, &timeTick, &channel);
  215. printf("Start wolfCrypt Benchmark\n");
  216. benchmark_test(NULL);
  217. printf("End wolfCrypt Benchmark\n");
  218. #endif
  219. #elif defined(TLS_CLIENT)
  220. #include "r_cmt_rx_if.h"
  221. #if defined(WOLFSSL_STATIC_MEMORY)
  222. if (wc_LoadStaticMemory(&heapHint, heapBufGen, sizeof(heapBufGen),
  223. WOLFMEM_GENERAL, 1) !=0) {
  224. printf("unable to load static memory.\n");
  225. return;
  226. }
  227. #endif /* WOLFSSL_STATIC_MEMORY */
  228. Open_tcp();
  229. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  230. SetTsiptlsKey();
  231. #endif
  232. do {
  233. if(cipherlist_sz > 0 ) printf("cipher : %s\n", cipherlist[i]);
  234. wolfSSL_TLS_client_init(cipherlist[i]);
  235. wolfSSL_TLS_client();
  236. i++;
  237. } while (i < cipherlist_sz);
  238. Close_tcp();
  239. #elif defined(TLS_SERVER)
  240. Open_tcp();
  241. #if defined(WOLFSSL_RENESAS_TSIP)
  242. SetTsiptlsKey();
  243. #endif
  244. wolfSSL_TLS_server_init(doClientCheck);
  245. wolfSSL_TLS_server();
  246. Close_tcp();
  247. #endif
  248. }
  249. #ifdef __cplusplus
  250. void abort(void)
  251. {
  252. }
  253. #endif