diff --git a/IDE/Renesas/cs+/Projects/t4_demo/README_jp.txt b/IDE/Renesas/cs+/Projects/t4_demo/README_jp.txt new file mode 100644 index 000000000..90d9df3e2 --- /dev/null +++ b/IDE/Renesas/cs+/Projects/t4_demo/README_jp.txt @@ -0,0 +1,115 @@ +wolfSSL/AlphaProjectボードデモ セットアップガイド + +このデモはRenesas CS+ v6.01、AP-RX71M-0A, wolfSSL 3.15.3 でテストしています。 + +セットアップ手順: + +1.ソフトウェアの入手 + - APボード付属のソフトウェア一式を適当なフォルダー下に解凍します。 + - 同じフォルダー下にwolfssl一式を解凍します。 + +2.wolfSSLのセットアップ + - CS*にてwolfssl\IDE\Renesas\cs+\Project下のwolfssl\lib.mtpjを開き +  wolfSSLライブラリーのビルドをします。 + - 同じフォルダの下のt4_demo.mtpjを開き、デモプログラムのビルドをします。 + このプログラムもライブラリー形式でビルドされます。 +  +3.AlphaProject側のセットアップ + デモはap_rx71m_0a_sample_cs\Sample\ap_rx71m_0a_ether_sample_csフォルダ下の + ap_rx71m_0a_ether_sample_cs.mtpjプロジェクトを利用します。 +  + - ap_rx71m_0a_sample_cs\Sample\ap_rx71m_0a_ether_sample_cs\srcフォルダ下の + AP_RX71M_0A.cファイルを開き、 + 97行目のecho_srv_init()の下にwolfSSL_init()を挿入します。 + +=== + sci_init(); + can_init(); + echo_srv_init(); + wolfSSL_init(); <- この行を挿入 +=== + + - ap_rx71m_0a_sample_cs\Sample\ap_rx71m_0a_ether_sample_cs\src\r_configファイル + を開き、スタックサイズとヒープサイズを以下のように設定します。 +  + 120行目 #pragma stacksize su=0x2000 + 139行目 #define BSP_CFG_HEAP_BYTES (0xa000) + + - IPアドレスのデフォルト値は以下のようになっています。 + 必要があれば、Sample\ap_rx71m_0a_ether_sample_cs\src\r_t4_rx\src\config_tcpudp.c + 内の139行目からの定義を変更します。 +  +=== +#define MY_IP_ADDR0 192,168,1,200 /* Local IP address */ +#define GATEWAY_ADDR0 192,168,1,254 /* Gateway address (invalid if all 0s) */ +#define SUBNET_MASK0 255,255,255,0 /* Subnet mask */ +=== + + - CS+でap_rx71m_0a_ether_sample_cs.mtpjプロジェクトを開き、wolfSSLとデモライブラリを + 登録します。CC-RX(ビルドツール)->リンク・オプションタブ->使用する以下の二つのファイル + を登録します。 + wolfssl\IDE\Renesas\cs+\Projects\wolfssl_lib\DefaultBuild\wolfssl_lib.lib + wolfssl\IDE\Renesas\cs+\Projects\t4_demo\DefaultBuild\t4_demo.lib + + - プロジェクトのビルド、ターゲットへのダウンロードをしたのち、表示->デバッグ・コンソール + からコンソールを表示させます。実行を開始するとコンソールに以下の表示が出力されます。 +  +=== + wolfSSL Demo +t: test, b: benchmark, s: server, or c : client +$ +=== + +tコマンド:各暗号化アルゴリズムの簡単なテストを実行します。所要のアルゴリズムが + 組み込まれているか確認することができます。組み込むアルゴリズムはビルドオプション + で変更することができます。詳しくはユーザマニュアルを参照してください。 +bコマンド:各暗号アルゴリズムごとの簡単なベンチマークを実行します。 +sコマンド:簡単なTLSサーバを起動します。起動するとビルド時のIPアドレス、 + ポート50000にてTLS接続を待ちます。 +cコマンド:簡単なTLSクライアントを起動します。起動すると第一アーギュメントで指定された + IPアドレス、第二アーギュメントで指定されたポートに対してTLS接続します。 + +いずれのコマンドも1回のみ実行します。繰り返し実行したい場合は、MPUをリセットして +再起動します。 + +4.対向テスト + デモのs、cコマンドを使って、他の機器と簡単な対向テストをすることができます。 + UbuntuなどのGCC, make環境、WindowsのVisual Studioなどで + 対向テスト用のサーバ、クライアントをビルドすることができます。 + + GCC,makeコマンド環境では、ダウンロード解凍したwolfsslのディレクトリ下で以下の + コマンドを発行すると、ライブラリ、テスト用のクライアント、サーバなど一式がビルド + されます。 +  + $ ./configure + $ make check +  + その後、以下のような指定でクライアントまたはサーバを起動して、ボード上の + デモと対向テストすることができます。 +  + PC側: + $ ./examples/server/server -b -d + ボード側: +  > c 11111 + + ボード側: +  > s + PC側:  + $ ./examples/client/client -h -p 50000 +  +  + WindowsのVisual Studioでは、ダウンロード解凍したwolfsslフォルダ下のwolfssl64.sln + を開き、ソリューションをビルドします。Debugフォルダ下にビルドされるclient.exeと + server.exeを利用します。 +  + PC側: + Debug> .\server -b -d + ボード側: +  > c 11111 + + ボード側: +  > s + PC側: + Debug> .\client -h -p 50000 + +以上、 \ No newline at end of file diff --git a/IDE/Renesas/cs+/Projects/t4_demo/wolf_client.c b/IDE/Renesas/cs+/Projects/t4_demo/wolf_client.c new file mode 100644 index 000000000..9fe2a3856 --- /dev/null +++ b/IDE/Renesas/cs+/Projects/t4_demo/wolf_client.c @@ -0,0 +1,192 @@ +/* wolf_client.c + * + * Copyright (C) 2006-2017 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include "r_t4_itcpip.h" + +#include "wolfssl/wolfcrypt/settings.h" +#include "wolfssl/ssl.h" +#include "wolfssl/certs_test.h" +#include "wolf_demo.h" + +ER t4_tcp_callback(ID cepid, FN fncd , VP p_parblk); + +static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx) +{ + int ret; + ID cepid; + + if(ctx != NULL)cepid = *(ID *)ctx; + else return WOLFSSL_CBIO_ERR_GENERAL; + + ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR); + if(ret > 0)return ret; + else return WOLFSSL_CBIO_ERR_GENERAL; +} + +static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx) +{ + int ret; + ID cepid; + + if(ctx != NULL)cepid = *(ID *)ctx; + else return WOLFSSL_CBIO_ERR_GENERAL; + + ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR); + if(ret == sz)return ret; + else return WOLFSSL_CBIO_ERR_GENERAL; +} + +static int getIPaddr(char *arg) +{ + int a1, a2, a3, a4; + if(sscanf(arg, "%d.%d.%d.%d", &a1, &a2, &a3, &a4) == 4) + return (a1 << 24) | (a2 << 16) | (a3 << 8) | a4; + else return 0; +} + +static int getPort(char *arg) +{ + int port; + if(sscanf(arg, "%d", &port) == 1) + return port; + else return 0; +} + +WOLFSSL_CTX *wolfSSL_TLS_client_init() +{ + + WOLFSSL_CTX* ctx; + #ifndef NO_FILESYSTEM + #ifdef USE_ECC_CERT + char *cert = "./certs/ca-ecc-cert.pem"; + #else + char *cert = "./certs/ca-cert.pem"; + #endif + #else + #ifdef USE_ECC_CERT + const unsigned char *cert = ca_ecc_der_256; + #define SIZEOF_CERT sizeof_ca_ecc_der_256 + #else + const unsigned char *cert = ca_cert_der_2048; + #define SIZEOF_CERT sizeof_ca_cert_der_2048 + #endif + #endif + + wolfSSL_Init(); + #ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); + #endif + + /* Create and initialize WOLFSSL_CTX */ + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method_ex((void *)NULL))) == NULL) { + printf("ERROR: failed to create WOLFSSL_CTX\n"); + return NULL; + } + + #if !defined(NO_FILESYSTEM) + if (wolfSSL_CTX_load_verify_locations(ctx, cert, 0) != SSL_SUCCESS) { + printf("ERROR: can't load \"%s\"\n", cert); + return NULL; + } + #else + if (wolfSSL_CTX_load_verify_buffer(ctx, cert, SIZEOF_CERT, SSL_FILETYPE_ASN1) != SSL_SUCCESS){ + printf("ERROR: can't load certificate data\n"); + return NULL; + } + #endif + + + /* Register callbacks */ + wolfSSL_SetIORecv(ctx, my_IORecv); + wolfSSL_SetIOSend(ctx, my_IOSend); + return (void *)ctx; + +} + + +void wolfSSL_TLS_client(void *v_ctx, func_args *args) +{ + ID cepid = 1; + ER ercd; + int ret; + WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)v_ctx; + WOLFSSL *ssl; + #define BUFF_SIZE 256 + static const char sendBuff[]= "Hello Server\n" ; + char rcvBuff[BUFF_SIZE] = {0}; + static T_IPV4EP my_addr = { 0, 0 }; + T_IPV4EP dst_addr; + + if(args->argc >= 2){ + if((dst_addr.ipaddr = getIPaddr(args->argv[1])) == 0){ + printf("ERROR: IP address\n"); + return; + } + if((dst_addr.portno = getPort(args->argv[2])) == 0){ + printf("ERROR: IP address\n"); + return; + } + } + + if((ercd = tcp_con_cep(cepid, &my_addr, &dst_addr, TMO_FEVR)) != E_OK) { + printf("ERROR TCP Connect: %d\n", ercd); + return; + } + + if((ssl = wolfSSL_new(ctx)) == NULL) { + printf("ERROR wolfSSL_new: %d\n", wolfSSL_get_error(ssl, 0)); + return; + } + + /* set callback context */ + wolfSSL_SetIOReadCtx(ssl, (void *)&cepid); + wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid); + + if(wolfSSL_connect(ssl) != SSL_SUCCESS) { + printf("ERROR SSL connect: %d\n", wolfSSL_get_error(ssl, 0)); + return; + } + + if (wolfSSL_write(ssl, sendBuff, strlen(sendBuff)) != strlen(sendBuff)) { + printf("ERROR SSL write: %d\n", wolfSSL_get_error(ssl, 0)); + return; + } + + if ((ret=wolfSSL_read(ssl, rcvBuff, BUFF_SIZE)) < 0) { + printf("ERROR SSL read: %d\n", wolfSSL_get_error(ssl, 0)); + return; + } + + rcvBuff[ret] = '\0' ; + printf("Recieved: %s\n", rcvBuff); + + /* frees all data before client termination */ + wolfSSL_free(ssl); + wolfSSL_CTX_free(ctx); + wolfSSL_Cleanup(); + + tcp_sht_cep(cepid); + tcp_cls_cep(cepid, TMO_FEVR); + + return; +} diff --git a/IDE/Renesas/cs+/Projects/t4_demo/wolf_demo.h b/IDE/Renesas/cs+/Projects/t4_demo/wolf_demo.h new file mode 100644 index 000000000..6f58c5380 --- /dev/null +++ b/IDE/Renesas/cs+/Projects/t4_demo/wolf_demo.h @@ -0,0 +1,18 @@ + + +typedef struct func_args +{ + int argc; + char **argv; + int return_code; +} func_args; + +void wolfSSL_init(void) ; +void wolfSSL_main(void) ; +WOLFSSL_CTX *wolfSSL_TLS_server_init(void); +WOLFSSL_CTX *wolfSSL_TLS_client_init(void); + +void benchmark_test(void *args); +void wolfcrypt_test(void *args); +void wolfSSL_TLS_client(void *ctx, func_args *args); +void wolfSSL_TLS_server(void *ctx, func_args *args); diff --git a/IDE/Renesas/cs+/Projects/t4_demo/wolf_main.c b/IDE/Renesas/cs+/Projects/t4_demo/wolf_main.c new file mode 100644 index 000000000..64126e933 --- /dev/null +++ b/IDE/Renesas/cs+/Projects/t4_demo/wolf_main.c @@ -0,0 +1,125 @@ +/* wolf_main.c + * + * Copyright (C) 2006-2017 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include "user_settings.h" +#include "wolfssl/ssl.h" +#include +#include + +#include "wolf_demo.h" + +static WOLFSSL_CTX *wolfSSL_sv_ctx; +static WOLFSSL_CTX *wolfSSL_cl_ctx; + +static long tick; +static void timeTick(void) +{ + tick++; +} + +#define FREQ 10000 /* Hz */ + +double current_time(int reset) +{ + if(reset) tick = 0 ; + return ((double)tick/FREQ) ; +} + +#define ARG_SZ 256 +static char argBuff[ARG_SZ]; +static int get_arg(func_args *args) +{ + int i; + char *arg = argBuff; + args->argc = 0; + + for(i=0; iargc; + case ' ': + *arg++ = '\0'; + while(*arg++ == ' '); /* Skip space */ + args->argv[++args->argc] = arg; + break; + default: + arg++; + } + } + return args->argc ; +} + +void wolfSSL_init() +{ + uint32_t channel; + R_CMT_CreatePeriodic(FREQ, &timeTick, &channel); + + wolfSSL_sv_ctx = wolfSSL_TLS_server_init(); + wolfSSL_cl_ctx = wolfSSL_TLS_client_init(); + wolfSSL_main(); +} + +void wolfSSL_main() +{ + int c; + func_args args = {0}; + + printf("wolfSSL Demo\nt: test, b: benchmark, s: server, or c : client\n$ "); + c = getchar(); + + switch(c) { + case 't': + get_arg(&args); + printf("Start wolfCrypt Test\n"); + wolfcrypt_test(&args); + printf("End wolfCrypt Test\n"); + break; + + case 'b': + get_arg(&args); + printf("Start wolfCrypt Benchmark\n"); + benchmark_test(NULL); + printf("End wolfCrypt Benchmark\n"); + break; + + case 'c': + if(get_arg(&args) < 0) + break; + printf("Start TLS Client(%s, %s)\n", args.argv[1], args.argv[2]); + wolfSSL_TLS_client(wolfSSL_cl_ctx, &args); + printf("End TLS Client\n"); + break; + + case 's': + if(get_arg(&args) < 0) + break; + printf("Start TLS Server\n"); + wolfSSL_TLS_server(wolfSSL_sv_ctx, &args); + printf("End TLS Server\n"); + break; + + default: + printf("Command Error\n"); + } +} diff --git a/IDE/Renesas/cs+/Projects/t4_demo/wolf_server.c b/IDE/Renesas/cs+/Projects/t4_demo/wolf_server.c new file mode 100644 index 000000000..4a99a3afb --- /dev/null +++ b/IDE/Renesas/cs+/Projects/t4_demo/wolf_server.c @@ -0,0 +1,173 @@ +/* wolf_server.c + * + * Copyright (C) 2006-2017 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ +#include +#include +#include "r_t4_itcpip.h" + +#include "wolfssl/wolfcrypt/settings.h" +#include "wolfssl/ssl.h" +#include "wolfssl/certs_test.h" +#include "wolf_demo.h" + +static int my_IORecv(WOLFSSL* ssl, char* buff, int sz, void* ctx) +{ + int ret; + ID cepid; + + if(ctx != NULL)cepid = *(ID *)ctx; + else return WOLFSSL_CBIO_ERR_GENERAL; + + ret = tcp_rcv_dat(cepid, buff, sz, TMO_FEVR); + if(ret == sz)return ret; + else return WOLFSSL_CBIO_ERR_GENERAL; +} + +static int my_IOSend(WOLFSSL* ssl, char* buff, int sz, void* ctx) +{ + int ret; + ID cepid; + + if(ctx != NULL)cepid = *(ID *)ctx; + else return WOLFSSL_CBIO_ERR_GENERAL; + + ret = tcp_snd_dat(cepid, buff, sz, TMO_FEVR); + if(ret == sz)return ret; + else return WOLFSSL_CBIO_ERR_GENERAL; +} + + +WOLFSSL_CTX *wolfSSL_TLS_server_init() +{ + + int ret; + WOLFSSL_CTX* ctx; + + #ifndef NO_FILESYSTEM + #ifdef USE_ECC_CERT + char *cert = "./certs/server-ecc-cert.pem"; + char *key = "./certs/server-ecc-key.pem"; + #else + char *cert = "./certs/server-cert.pem"; + char *key = "./certs/server-key.pem"; + #endif + #else + #ifdef USE_ECC_CERT + char *cert = serv_ecc_der_256; + int sizeof_cert = sizeof_serv_ecc_der_256; + char *cert = serv_ecc_key_der_256; + int sizeof_key = sizeof_serv_ecc_key_der_256; + #else + const unsigned char *cert = server_cert_der_2048; + #define sizeof_cert sizeof_server_cert_der_2048 + const unsigned char *key = server_key_der_2048; + #define sizeof_key sizeof_server_key_der_2048 + #endif + #endif + + + wolfSSL_Init(); + #ifdef DEBUG_WOLFSSL + wolfSSL_Debugging_ON(); + #endif + + /* Create and initialize WOLFSSL_CTX */ + if ((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method_ex((void *)NULL))) == NULL) { + printf("ERROR: failed to create WOLFSSL_CTX\n"); + return NULL; + } + + #if !defined(NO_FILESYSTEM) + ret = wolfSSL_CTX_use_certificate_file(ctx, cert, 0); + #else + ret = wolfSSL_CTX_use_certificate_buffer(ctx, cert, sizeof_cert, SSL_FILETYPE_ASN1); + #endif + if (ret != SSL_SUCCESS) { + printf("Error %d loading server-cert!\n", ret); + return NULL; + + } + + /* Load server key into WOLFSSL_CTX */ + #if !defined(NO_FILESYSTEM) + ret = wolfSSL_CTX_use_PrivateKey_file(ctx, key, 0); + #else + ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, key, sizeof_key, SSL_FILETYPE_ASN1); + #endif + if (ret != SSL_SUCCESS) { + printf("Error %d loading server-key!\n", ret); + return NULL; + } + + /* Register callbacks */ + wolfSSL_SetIORecv(ctx, my_IORecv); + wolfSSL_SetIOSend(ctx, my_IOSend); + return ctx; + +} + +void wolfSSL_TLS_server(void *v_ctx, func_args *args) +{ + ID cepid = 1; + ID repid = 1; + ER ercd; + WOLFSSL_CTX *ctx = (WOLFSSL_CTX *)v_ctx; + (void) args; + + WOLFSSL *ssl; + int len; + #define BUFF_SIZE 256 + char buff[BUFF_SIZE]; + T_IPV4EP dst_addr = {0, 0}; + + if((ercd = tcp_acp_cep(cepid, repid, &dst_addr, TMO_FEVR)) != E_OK) { + printf("ERROR TCP Accept: %d\n", ercd); + return; + } + + if((ssl = wolfSSL_new(ctx)) == NULL) { + printf("ERROR: failed wolfSSL_new\n"); + return; + } + + wolfSSL_SetIOReadCtx(ssl, (void *)&cepid); + wolfSSL_SetIOWriteCtx(ssl, (void *)&cepid); + + if (wolfSSL_accept(ssl) < 0) { + printf("ERROR: SSL Accept(%d)\n", wolfSSL_get_error(ssl, 0)); + return; + } + + if ((len = wolfSSL_read(ssl, buff, sizeof(buff) - 1)) < 0) { + printf("ERROR: SSL Read(%d)\n", wolfSSL_get_error(ssl, 0)); + return; + } + + buff[len] = '\0'; + printf("Recieved: %s\n", buff); + + if (wolfSSL_write(ssl, buff, len) != len) { + printf("ERROR: SSL Wirte(%d)\n", wolfSSL_get_error(ssl, 0)); + return; + } + + wolfSSL_free(ssl); + tcp_sht_cep(cepid); +}