Skip to content

Commit f8604da

Browse files
committed
change to use a cutom random generation func for PRNG
1 parent 210fff5 commit f8604da

6 files changed

Lines changed: 101 additions & 85 deletions

File tree

IDE/Renesas/e2studio/RX72N/EnvisionKit/Simple/test/src/client/simple_tls_tsip_client.c

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void wolfSSL_TLS_client_init(const char* cipherlist)
103103
char *cert = "./certs/ca-cert.pem";
104104
#endif
105105
#else
106-
#if defined(USE_ECC_CERT) && defined(USE_CERT_BUFFERS_256)
106+
#if defined(USE_ECC_CERT) && defined(USE_CERT_BUFFERS_256)
107107
const unsigned char *cert = ca_ecc_cert_der_256;
108108
#define SIZEOF_CERT sizeof_ca_ecc_cert_der_256
109109
#else
@@ -118,7 +118,7 @@ void wolfSSL_TLS_client_init(const char* cipherlist)
118118
#endif
119119

120120
/* Create and initialize WOLFSSL_CTX */
121-
if ((client_ctx =
121+
if ((client_ctx =
122122
wolfSSL_CTX_new(wolfSSLv23_client_method_ex((void *)NULL))) == NULL) {
123123
printf("ERROR: failed to create WOLFSSL_CTX\n");
124124
return;
@@ -134,7 +134,7 @@ void wolfSSL_TLS_client_init(const char* cipherlist)
134134
return;
135135
}
136136
#else
137-
if (wolfSSL_CTX_load_verify_buffer(client_ctx, cert, SIZEOF_CERT,
137+
if (wolfSSL_CTX_load_verify_buffer(client_ctx, cert, SIZEOF_CERT,
138138
SSL_FILETYPE_ASN1) != SSL_SUCCESS){
139139
printf("ERROR: can't load certificate data\n");
140140
return;
@@ -151,24 +151,6 @@ void wolfSSL_TLS_client_init(const char* cipherlist)
151151
return;
152152
}
153153

154-
/* set client private key data */
155-
#if defined(WOLFSSL_TLS13) && defined(SIMPLE_TLS_TSIP_CLIENT)
156-
if (tsip_set_clientPrivateKeyEnc(
157-
g_key_block_data.encrypted_user_ecc256_private_key,
158-
TSIP_ECCP256) != 0) {
159-
printf("ERROR: can't load client-private key\n");
160-
return;
161-
}
162-
#else
163-
if (wolfSSL_CTX_use_PrivateKey_buffer(client_ctx,
164-
ecc_clikey_der_256,
165-
sizeof_ecc_clikey_der_256,
166-
SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
167-
printf("ERROR: can't load private-key data.\n");
168-
return;
169-
}
170-
#endif /* WOLFSSL_TLS13 */
171-
172154
#else
173155
if (wolfSSL_CTX_use_certificate_chain_buffer_format(client_ctx,
174156
client_cert_der_2048,
@@ -195,15 +177,15 @@ void wolfSSL_TLS_client_init(const char* cipherlist)
195177
wolfSSL_SetIOSend(client_ctx, my_IOSend);
196178

197179
/* use specific cipher */
198-
if (cipherlist != NULL &&
180+
if (cipherlist != NULL &&
199181
wolfSSL_CTX_set_cipher_list(client_ctx, cipherlist) != WOLFSSL_SUCCESS) {
200182
wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
201183
printf("client can't set cipher list");
202184
return;
203185
}
204-
186+
205187
#if defined(WOLFSSL_TLS13)
206-
if (wolfSSL_CTX_UseSupportedCurve(client_ctx, WOLFSSL_ECC_SECP256R1)
188+
if (wolfSSL_CTX_UseSupportedCurve(client_ctx, WOLFSSL_ECC_SECP256R1)
207189
!= WOLFSSL_SUCCESS) {
208190
wolfSSL_CTX_free(client_ctx); client_ctx = NULL;
209191
printf("client can't set use supported curves\n");
@@ -222,11 +204,11 @@ void wolfSSL_TLS_client( )
222204

223205
#define BUFF_SIZE 256
224206
static const char sendBuff[]= "Hello Server\n" ;
225-
207+
226208
char rcvBuff[BUFF_SIZE] = {0};
227-
209+
228210
static T_IPV4EP my_addr = { 0, 0 };
229-
211+
230212
T_IPV4EP dst_addr;
231213

232214
if((dst_addr.ipaddr = getIPaddr(SIMPLE_TLSSEVER_IP)) == 0){
@@ -248,9 +230,45 @@ void wolfSSL_TLS_client( )
248230
goto out;
249231
}
250232

251-
#ifdef SIMPLE_TLS_TSIP_CLIENT
233+
#ifdef SIMPLE_TLS_TSIP_CLIENT
252234
tsip_set_callback_ctx(ssl, &userContext);
235+
#endif
236+
237+
/* set client private key data */
238+
#if defined(WOLFSSL_TLS13) && defined(SIMPLE_TLS_TSIP_CLIENT)
239+
#if defined(USE_ECC_CERT)
240+
if (tsip_use_PrivateKey_buffer_TLS(ssl,
241+
(const char*)g_key_block_data.encrypted_user_ecc256_private_key,
242+
sizeof(g_key_block_data.encrypted_user_ecc256_private_key),
243+
TSIP_ECCP256) != 0) {
244+
printf("ERROR: can't load client-private key\n");
245+
return;
246+
}
247+
#else
248+
if (tsip_use_PrivateKey_buffer_TLS(ssl,
249+
(const char*)g_key_block_data.encrypted_user_rsa2048_private_key,
250+
sizeof(g_key_block_data.encrypted_user_rsa2048_private_key),
251+
TSIP_RSA2048) != 0) {
252+
printf("ERROR: can't load client-private key\n");
253+
return;
254+
}
255+
ret = tsip_use_PublicKey_buffer_TLS(ssl,
256+
(const char*)g_key_block_data.encrypted_user_rsa2048_public_key,
257+
sizeof(g_key_block_data.encrypted_user_rsa2048_public_key), TSIP_RSA2048);
258+
if (ret != 0) {
259+
printf("ERROR tsip_use_PublicKey_buffer: %d\n", ret);
260+
return;
261+
}
253262
#endif
263+
#else
264+
if (wolfSSL_use_PrivateKey_buffer(ssl,
265+
ecc_clikey_der_256,
266+
sizeof_ecc_clikey_der_256,
267+
SSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
268+
printf("ERROR: can't load private-key data.\n");
269+
return;
270+
}
271+
#endif /* WOLFSSL_TLS13 */
254272

255273
/* set callback context */
256274
wolfSSL_SetIOReadCtx(ssl, (void *)&cepid);

IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/user_settings.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/*-- Renesas TSIP usage and its version ---------------------------------------
3232
*
3333
* "WOLFSSL_RENESAS_TSIP" definition makes wolfSSL to use H/W acceleration
34-
* for cipher operations.
34+
* for cipher operations.
3535
* TSIP definition asks to have its version number.
3636
* "WOLFSSL_RENESAS_TSIP_VER" takes following value:
3737
* 106: TSIPv1.06
@@ -54,16 +54,16 @@
5454
*
5555
* wolfSSL supports TLSv1.2 by default. In case you want your system to support
5656
* TLSv1.3, uncomment line below.
57-
*
57+
*
5858
*----------------------------------------------------------------------------*/
5959
#define WOLFSSL_TLS13
6060

6161

6262
/*-- Operating System related definitions --------------------------------------
63-
*
63+
*
6464
* In case any real-time OS is used, define its name(e.g. FREERTOS).
6565
* Otherwise, define "SINGLE_THREADED". They are exclusive each other.
66-
*
66+
*
6767
*----------------------------------------------------------------------------*/
6868
#if !defined(RENESAS_T4_USE)
6969
#define FREERTOS
@@ -114,23 +114,23 @@
114114

115115
/* USE_ECC_CERT
116116
* This macro is for selecting root CA certificate to load, it is valid only
117-
* in example applications. wolfSSL does not refer this macro.
118-
* If you want to use cipher suites including ECDSA authentication in
117+
* in example applications. wolfSSL does not refer this macro.
118+
* If you want to use cipher suites including ECDSA authentication in
119119
* the example applications with TSIP, enable this macro.
120-
* In TSIP 1.13 or later version, following cipher suites are
120+
* In TSIP 1.13 or later version, following cipher suites are
121121
* available:
122122
* - TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
123123
* - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SAH256
124-
*
124+
*
125125
* Note that, this macro disables cipher suites including RSA
126126
* authentication such as:
127127
* - TLS_RSA_WITH_AES_128_CBC_SHA
128-
* - TLS_RSA_WITH_AES_256_CBC_SHA
128+
* - TLS_RSA_WITH_AES_256_CBC_SHA
129129
* - TLS_RSA_WITH_AES_128_CBC_SHA256
130130
* - TLS_RSA_WITH_AES_256_CBC_SHA256
131131
* - TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
132132
* - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA256
133-
*
133+
*
134134
*/
135135
#define USE_ECC_CERT
136136

@@ -139,14 +139,14 @@
139139
*/
140140
/*#define WOLFSSL_CHECK_SIG_FAULTS*/
141141

142-
/* In this example application, Root CA cert buffer named
143-
* "ca_ecc_cert_der_256" is used under the following macro definition
142+
/* In this example application, Root CA cert buffer named
143+
* "ca_ecc_cert_der_256" is used under the following macro definition
144144
* for ECDSA.
145145
*/
146146
#define USE_CERT_BUFFERS_256
147147

148-
/* In this example application, Root CA cert buffer named
149-
* "ca_cert_der_2048" is used under the following macro definition
148+
/* In this example application, Root CA cert buffer named
149+
* "ca_cert_der_2048" is used under the following macro definition
150150
* for RSA authentication.
151151
*/
152152
#define USE_CERT_BUFFERS_2048
@@ -159,25 +159,25 @@
159159

160160
#define WOLFSSL_SMALL_STACK
161161

162-
/*
162+
/*
163163
* -- "NO_ASN_TIME" macro is to avoid certificate expiration validation --
164-
*
165-
* Note. In your actual products, do not forget to comment-out
164+
*
165+
* Note. In your actual products, do not forget to comment-out
166166
* "NO_ASN_TIME" macro. And prepare time function to get calendar time,
167-
* otherwise, certificate expiration validation will not work.
167+
* otherwise, certificate expiration validation will not work.
168168
*/
169169
/*#define NO_ASN_TIME*/
170-
170+
171171
#define NO_MAIN_DRIVER
172172
#define BENCH_EMBEDDED
173-
#define NO_WOLFSSL_DIR
173+
#define NO_WOLFSSL_DIR
174174
#define WOLFSSL_NO_CURRDIR
175175
#define NO_FILESYSTEM
176176
#define WOLFSSL_LOG_PRINTF
177177
#define WOLFSSL_HAVE_MIN
178178
#define WOLFSSL_HAVE_MAX
179179
#define NO_WRITEV
180-
180+
181181

182182
#define WOLFSSL_USER_CURRTIME /* for benchmark */
183183
#define TIME_OVERRIDES
@@ -210,7 +210,7 @@
210210

211211
/*-- Definitions for functionality negation -----------------------------------
212212
*
213-
*
213+
*
214214
*----------------------------------------------------------------------------*/
215215

216216
/*#define NO_RENESAS_TSIP_CRYPT*/
@@ -276,3 +276,5 @@
276276

277277
/*-- strcasecmp */
278278
#define XSTRCASECMP(s1,s2) strcmp((s1),(s2))
279+
280+
#define CUSTOM_RAND_GENERATE_BLOCK wc_tsip_GenerateRandBlock

src/tls.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7434,12 +7434,6 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
74347434
}
74357435

74367436
if (kse->key == NULL) {
7437-
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
7438-
ret = tsip_Tls13GenEccKeyPair(ssl, kse);
7439-
if (ret != CRYPTOCB_UNAVAILABLE) {
7440-
return ret;
7441-
}
7442-
#endif
74437437
/* Allocate an ECC key to hold private key. */
74447438
kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
74457439
if (kse->key == NULL) {
@@ -7454,6 +7448,12 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
74547448
kse->keyLen = keySize;
74557449
kse->pubKeyLen = keySize * 2 + 1;
74567450

7451+
#if defined(WOLFSSL_RENESAS_TSIP_TLS)
7452+
ret = tsip_Tls13GenEccKeyPair(ssl, kse);
7453+
if (ret != CRYPTOCB_UNAVAILABLE) {
7454+
return ret;
7455+
}
7456+
#endif
74577457
/* setting eccKey means okay to call wc_ecc_free */
74587458
eccKey = (ecc_key*)kse->key;
74597459

wolfcrypt/src/port/Renesas/renesas_tsip_util.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,30 @@ WOLFSSL_LOCAL void tsip_Close(void)
28002800
WOLFSSL_LEAVE("tsip_Close", 0);
28012801
}
28022802

2803+
int wc_tsip_GenerateRandBlock(byte* output, word32 sz)
2804+
{
2805+
/* Generate PRNG based on NIST SP800-90A AES CTR-DRBG */
2806+
int ret = 0;
2807+
word32 buffer[4];
2808+
2809+
while (sz > 0) {
2810+
word32 len = sizeof(buffer);
2811+
2812+
if (sz < len) {
2813+
len = sz;
2814+
}
2815+
/* return 4 words random number*/
2816+
ret = R_TSIP_GenerateRandomNumber((uint32_t*)buffer);
2817+
if(ret == TSIP_SUCCESS) {
2818+
XMEMCPY(output, &buffer, len);
2819+
output += len;
2820+
sz -= len;
2821+
} else
2822+
return ret;
2823+
}
2824+
return ret;
2825+
}
2826+
28032827
#if (WOLFSSL_RENESAS_TSIP_VER>=109)
28042828
void tsip_inform_user_keys_ex(
28052829
byte* encrypted_provisioning_key,

wolfcrypt/src/random.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,35 +3445,6 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
34453445
return 0;
34463446
}
34473447

3448-
#elif defined(WOLFSSL_RENESAS_TSIP)
3449-
#if defined(WOLFSSL_RENESA_TSIP_IAREWRX)
3450-
#include "r_bsp/mcu/all/r_rx_compiler.h"
3451-
#endif
3452-
#include "r_bsp/platform.h"
3453-
#include "r_tsip_rx_if.h"
3454-
3455-
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3456-
{
3457-
int ret = 0;
3458-
word32 buffer[4];
3459-
3460-
while (sz > 0) {
3461-
word32 len = sizeof(buffer);
3462-
3463-
if (sz < len) {
3464-
len = sz;
3465-
}
3466-
/* return 4 words random number*/
3467-
ret = R_TSIP_GenerateRandomNumber((uint32_t*)buffer);
3468-
if(ret == TSIP_SUCCESS) {
3469-
XMEMCPY(output, &buffer, len);
3470-
output += len;
3471-
sz -= len;
3472-
} else
3473-
return ret;
3474-
}
3475-
return ret;
3476-
}
34773448

34783449
#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_TRNG)
34793450
#include "hal_data.h"

wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ WOLFSSL_LOCAL int wc_tsip_MakeRsaKey(int size, void* ctx);
639639
WOLFSSL_LOCAL int wc_tsip_RsaVerifyPkcs(wc_CryptoInfo* info,
640640
TsipUserCtx* tuc);
641641

642+
WOLFSSL_LOCAL int wc_tsip_GenerateRandBlock(byte* output, word32 size);
642643

643644
#if defined(WOLFSSL_RENESAS_TSIP_CRYPT_DEBUG)
644645
byte *ret2err(word32 ret);

0 commit comments

Comments
 (0)