Skip to content

Commit f7c1f77

Browse files
committed
Progress with TLS v1.3 using X25519
1 parent 8c483e4 commit f7c1f77

11 files changed

Lines changed: 193 additions & 41 deletions

File tree

.wolfssl_known_macro_extras

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ WC_ASYNC_NO_SHA256
616616
WC_ASYNC_NO_SHA3
617617
WC_ASYNC_NO_SHA384
618618
WC_ASYNC_NO_SHA512
619+
WC_ASYNC_NO_X25519
619620
WC_ASYNC_THREAD_BIND
620621
WC_CACHE_RESISTANT_BASE64_TABLE
621622
WC_DILITHIUM_CACHE_PRIV_VECTORS

examples/async/async_client.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ static void usage(const char* prog)
157157
printf("usage: %s [--ecc|--x25519] [host] [port]\n", prog);
158158
}
159159

160+
static const char* group_name(word16 group)
161+
{
162+
switch (group) {
163+
case WOLFSSL_ECC_SECP256R1:
164+
return "secp256r1";
165+
case WOLFSSL_ECC_X25519:
166+
return "x25519";
167+
default:
168+
return "unknown";
169+
}
170+
}
171+
160172
static int parse_client_args(int argc, char** argv,
161173
const char** host, int* port, word16* group)
162174
{
@@ -214,11 +226,14 @@ int client_async_test(int argc, char** argv)
214226
const char* host = NULL;
215227
int port = 0;
216228
word16 group = WOLFSSL_ECC_SECP256R1;
229+
const char* mode = NULL;
217230

218231
if (parse_client_args(argc, argv, &host, &port, &group) != 0) {
219232
usage(argv[0]);
220233
return 0;
221234
}
235+
mode = group_name(group);
236+
printf("Async client mode: %s (keyshare 0x%04x)\n", mode, group);
222237

223238
{
224239
const char* ready = getenv(WOLFSSL_ASYNC_READYFILE_ENV);
@@ -320,6 +335,13 @@ int client_async_test(int argc, char** argv)
320335
goto out;
321336
}
322337

338+
{
339+
const char* cipher = wolfSSL_get_cipher_name(ssl);
340+
const char* curve = wolfSSL_get_curve_name(ssl);
341+
printf("Negotiated cipher: %s\n", cipher != NULL ? cipher : "unknown");
342+
printf("Negotiated group: %s\n", curve != NULL ? curve : "unknown");
343+
}
344+
323345
tx_len = XSNPRINTF(tx, sizeof(tx),
324346
"GET / HTTP/1.1\r\n"
325347
"Host: %s\r\n"

examples/async/async_server.c

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ static void usage(const char* prog)
117117
printf("usage: %s [--ecc|--x25519] [port]\n", prog);
118118
}
119119

120+
static const char* group_name(word16 group)
121+
{
122+
switch (group) {
123+
case WOLFSSL_ECC_SECP256R1:
124+
return "secp256r1";
125+
case WOLFSSL_ECC_X25519:
126+
return "x25519";
127+
default:
128+
return "unknown";
129+
}
130+
}
131+
120132
static int parse_server_args(int argc, char** argv, int* port, word16* group)
121133
{
122134
int i;
@@ -160,6 +172,7 @@ int server_async_test(int argc, char** argv)
160172
int port = DEFAULT_PORT;
161173
word16 group = WOLFSSL_ECC_SECP256R1;
162174
int err = 0;
175+
const char* mode = NULL;
163176
#ifdef WOLFSSL_ASYNC_CRYPT
164177
int devId = INVALID_DEVID;
165178
#endif
@@ -183,6 +196,8 @@ int server_async_test(int argc, char** argv)
183196
usage(argv[0]);
184197
return 0;
185198
}
199+
mode = group_name(group);
200+
printf("Async server mode: %s (keyshare 0x%04x)\n", mode, group);
186201

187202
/* Initialize the server address struct with zeros */
188203
memset(&servAddr, 0, sizeof(servAddr));
@@ -267,20 +282,42 @@ int server_async_test(int argc, char** argv)
267282
wolfSSL_SetIORecv(ctx, NET_IO_RECV_CB);
268283
wolfSSL_SetIOSend(ctx, NET_IO_SEND_CB);
269284

270-
/* Load server certificates into WOLFSSL_CTX */
271-
ret = wolfSSL_CTX_use_certificate_buffer(ctx, serv_ecc_der_256,
272-
sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
273-
if (ret != WOLFSSL_SUCCESS) {
274-
fprintf(stderr, "ERROR: failed to load ECC server cert buffer.\n");
285+
if (group == WOLFSSL_ECC_X25519) {
286+
#ifdef HAVE_ED25519
287+
ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_ed25519_cert,
288+
sizeof_server_ed25519_cert, WOLFSSL_FILETYPE_ASN1);
289+
if (ret != WOLFSSL_SUCCESS) {
290+
fprintf(stderr,
291+
"ERROR: failed to load ED25519 server cert buffer.\n");
292+
goto exit;
293+
}
294+
295+
ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_ed25519_key,
296+
sizeof_server_ed25519_key, WOLFSSL_FILETYPE_ASN1);
297+
if (ret != WOLFSSL_SUCCESS) {
298+
fprintf(stderr,
299+
"ERROR: failed to load ED25519 server key buffer.\n");
300+
goto exit;
301+
}
302+
#else
303+
fprintf(stderr, "ERROR: --x25519 requires HAVE_ED25519 for certs\n");
275304
goto exit;
305+
#endif
276306
}
307+
else {
308+
ret = wolfSSL_CTX_use_certificate_buffer(ctx, serv_ecc_der_256,
309+
sizeof_serv_ecc_der_256, WOLFSSL_FILETYPE_ASN1);
310+
if (ret != WOLFSSL_SUCCESS) {
311+
fprintf(stderr, "ERROR: failed to load ECC server cert buffer.\n");
312+
goto exit;
313+
}
277314

278-
/* Load server key into WOLFSSL_CTX */
279-
ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, ecc_key_der_256,
280-
sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
281-
if (ret != WOLFSSL_SUCCESS) {
282-
fprintf(stderr, "ERROR: failed to load ECC server key buffer.\n");
283-
goto exit;
315+
ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, ecc_key_der_256,
316+
sizeof_ecc_key_der_256, WOLFSSL_FILETYPE_ASN1);
317+
if (ret != WOLFSSL_SUCCESS) {
318+
fprintf(stderr, "ERROR: failed to load ECC server key buffer.\n");
319+
goto exit;
320+
}
284321
}
285322

286323
/* Continue to accept clients until mShutdown is issued */
@@ -357,6 +394,14 @@ int server_async_test(int argc, char** argv)
357394
goto exit;
358395
}
359396

397+
{
398+
const char* cipher = wolfSSL_get_cipher_name(ssl);
399+
const char* curve = wolfSSL_get_curve_name(ssl);
400+
printf("Negotiated cipher: %s\n",
401+
cipher != NULL ? cipher : "unknown");
402+
printf("Negotiated group: %s\n",
403+
curve != NULL ? curve : "unknown");
404+
}
360405
printf("Client connected successfully\n");
361406

362407
/* Read the client data into our buff array */
@@ -434,6 +479,11 @@ int server_async_test(int argc, char** argv)
434479
goto exit;
435480
}
436481

482+
#ifdef WOLFSSL_DEBUG_NONBLOCK
483+
printf("WANT_READ/WRITE count: %d\n", wouldblock_count);
484+
printf("WC_PENDING_E count: %d\n", pending_count);
485+
#endif
486+
437487
/* Cleanup after this connection */
438488
wolfSSL_shutdown(ssl);
439489
if (ssl) {

examples/async/user_settings.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/* user_settings.h
2+
*
3+
* Copyright (C) 2006-2026 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 3 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+
122
/* Bare-metal user settings for TLS 1.3 client with WOLFSSL_USER_IO. */
223
#ifndef WOLFSSL_USER_SETTINGS_H
324
#define WOLFSSL_USER_SETTINGS_H
@@ -17,19 +38,21 @@
1738
#define WOLFSSL_SP_NO_MALLOC
1839
#define ECC_TIMING_RESISTANT
1940

41+
#define HAVE_ED25519
2042
#define HAVE_CURVE25519
2143
#define CURVE25519_SMALL
44+
#define ED25519_SMALL
2245
#define WC_X25519_NONBLOCK
2346

2447
#define WOLFSSL_ASYNC_CRYPT
2548
#define WOLFSSL_ASYNC_CRYPT_SW
2649
#define WC_NO_ASYNC_THREADING
2750
#define HAVE_WOLF_BIGINT
2851

29-
#define NO_RSA
30-
3152
#define HAVE_AESGCM
3253

54+
#define WOLFSSL_SHA512
55+
3356
#define WOLFSSL_TLS13
3457
#define WOLFSSL_NO_TLS12
3558
#define HAVE_HKDF
@@ -48,7 +71,8 @@ extern int posix_getdevrandom(unsigned char *out, unsigned int sz);
4871
#define CUSTOM_RAND_GENERATE_SEED posix_getdevrandom
4972
#endif
5073

51-
/* Minimal feature set - explicitly disable unwanted algorithms. */
74+
/* Minimal feature set - explicitly disable unwanted algorithms */
75+
#define NO_RSA
5276
#define NO_DH
5377
#define NO_DSA
5478
#define WOLFSSL_NO_SHAKE256
@@ -59,8 +83,10 @@ extern int posix_getdevrandom(unsigned char *out, unsigned int sz);
5983
#define NO_SHA
6084
#define NO_OLD_TLS
6185

62-
/* Debugging helper. */
63-
//#define DEBUG_WOLFSSL
86+
/* Debugging */
87+
#if 0
88+
#define DEBUG_WOLFSSL
89+
#endif
6490
#define WOLFSSL_DEBUG_NONBLOCK
6591

6692
#endif /* WOLFSSL_USER_SETTINGS_H */

gencertbuf.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
# Used with HAVE_ED25519 define.
4141
my @fileList_ed = (
4242
[ "./certs/ed25519/server-ed25519.der", "server_ed25519_cert" ],
43-
[ "./certs/ed25519/server-ed25519-key.der", "server_ed25519_key" ],
43+
[ "./certs/ed25519/server-ed25519-priv.der", "server_ed25519_key" ],
4444
[ "./certs/ed25519/ca-ed25519.der", "ca_ed25519_cert" ],
4545
[ "./certs/ed25519/client-ed25519.der", "client_ed25519_cert" ],
46-
[ "./certs/ed25519/client-ed25519-key.der", "client_ed25519_key" ]
46+
[ "./certs/ed25519/client-ed25519-priv.der", "client_ed25519_key" ]
4747
);
4848

4949
# x25519 keys and certs

src/internal.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8190,8 +8190,7 @@ void FreeKey(WOLFSSL* ssl, int type, void** pKey)
81908190
#ifdef HAVE_CURVE25519
81918191
case DYNAMIC_TYPE_CURVE25519:
81928192
#if defined(WC_X25519_NONBLOCK) && \
8193-
defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8194-
defined(WC_ASYNC_ENABLE_X25519)
8193+
defined(WOLFSSL_ASYNC_CRYPT_SW)
81958194
if (((curve25519_key*)*pKey)->nbCtx != NULL) {
81968195
XFREE(((curve25519_key*)*pKey)->nbCtx, ssl->heap,
81978196
DYNAMIC_TYPE_TMP_BUFFER);
@@ -8249,10 +8248,9 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
82498248
#ifdef HAVE_CURVE25519
82508249
curve25519_key* x25519Key;
82518250
#endif /* HAVE_CURVE25519 */
8252-
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8253-
defined(WC_ASYNC_ENABLE_X25519)
8251+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
82548252
x25519_nb_ctx_t* x25519NbCtx;
8255-
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW && WC_ASYNC_ENABLE_X25519 */
8253+
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW */
82568254

82578255
if (ssl == NULL || pKey == NULL) {
82588256
return BAD_FUNC_ARG;
@@ -8367,8 +8365,7 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83678365
case DYNAMIC_TYPE_CURVE25519:
83688366
x25519Key = (curve25519_key*)*pKey;
83698367
ret = wc_curve25519_init_ex(x25519Key, ssl->heap, ssl->devId);
8370-
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8371-
defined(WC_ASYNC_ENABLE_X25519)
8368+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
83728369
if (ret == 0) {
83738370
x25519NbCtx = (x25519_nb_ctx_t*)XMALLOC(sizeof(x25519_nb_ctx_t),
83748371
ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
@@ -8382,8 +8379,7 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83828379
}
83838380
}
83848381
}
8385-
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8386-
WC_ASYNC_ENABLE_X25519 */
8382+
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW */
83878383
break;
83888384
#endif /* HAVE_CURVE25519 */
83898385
#ifdef HAVE_ED448

src/tls.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8002,7 +8002,7 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80028002

80038003
/* Make an Curve25519 key. */
80048004
ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
8005-
INVALID_DEVID);
8005+
ssl->devId);
80068006
if (ret == 0) {
80078007
/* setting "key" means okay to call wc_curve25519_free */
80088008
key = (curve25519_key*)kse->key;
@@ -8014,6 +8014,13 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80148014
#endif
80158015
{
80168016
ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
8017+
8018+
/* Handle async pending response */
8019+
#ifdef WOLFSSL_ASYNC_CRYPT
8020+
if (ret == WC_NO_ERR_TRACE(WC_PENDING_E)) {
8021+
return wolfSSL_AsyncPush(ssl, &key->asyncDev);
8022+
}
8023+
#endif /* WOLFSSL_ASYNC_CRYPT */
80178024
}
80188025
}
80198026
}

wolfcrypt/src/async.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ static int wolfAsync_DoSw(WC_ASYNC_DEV* asyncDev)
268268
break;
269269
}
270270
#endif /* !NO_DES3 */
271+
#ifdef HAVE_CURVE25519
272+
case ASYNC_SW_X25519_MAKE:
273+
{
274+
ret = wc_curve25519_make_key(
275+
(WC_RNG*)sw->x25519Make.rng,
276+
sw->x25519Make.size,
277+
(curve25519_key*)sw->x25519Make.key
278+
);
279+
break;
280+
}
281+
case ASYNC_SW_X25519_SHARED_SEC:
282+
{
283+
ret = wc_curve25519_shared_secret_ex(
284+
(curve25519_key*)sw->x25519SharedSec.priv,
285+
(curve25519_key*)sw->x25519SharedSec.pub,
286+
sw->x25519SharedSec.out,
287+
sw->x25519SharedSec.outLen,
288+
sw->x25519SharedSec.endian
289+
);
290+
break;
291+
}
292+
#endif /* HAVE_CURVE25519 */
271293
default:
272294
WOLFSSL_MSG("Invalid async crypt SW type!");
273295
ret = BAD_FUNC_ARG;

wolfcrypt/src/curve25519.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ const curve25519_set_type curve25519_sets[] = {
8686

8787
#if (!defined(WOLFSSL_CURVE25519_USE_ED25519) && \
8888
!(defined(CURVED25519_X64) || (defined(WOLFSSL_ARMASM) && \
89-
defined(__aarch64__)))) || defined(WOLFSSL_CURVE25519_BLINDING)
89+
defined(__aarch64__)))) || defined(WOLFSSL_CURVE25519_BLINDING) || \
90+
defined(WC_X25519_NONBLOCK)
9091
static const word32 kCurve25519BasePoint[CURVE25519_KEYSIZE/sizeof(word32)] = {
9192
#ifdef BIG_ENDIAN_ORDER
9293
0x09000000

0 commit comments

Comments
 (0)