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