Skip to content

Commit 41137ee

Browse files
committed
Add tests for async with crypto callbacks
1 parent 9102df3 commit 41137ee

7 files changed

Lines changed: 107 additions & 14 deletions

File tree

.github/workflows/async-examples.yml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21+
async_mode: ['sw', 'cryptocb']
2122
extra_cflags:
2223
- ''
2324
- '-DWOLFSSL_SMALL_CERT_VERIFY'
2425
- '-DWOLFSSL_STATIC_MEMORY'
25-
name: Async Examples (${{ matrix.extra_cflags || 'default' }})
26+
name: Async Examples (${{ matrix.async_mode }}, ${{ matrix.extra_cflags || 'default' }})
2627
steps:
2728
- uses: actions/checkout@v4
2829
name: Checkout wolfSSL
2930

3031
- name: Build async examples (no configure)
3132
run: |
3233
make -C examples/async clean
33-
make -C examples/async EXTRA_CFLAGS="${{ matrix.extra_cflags }}"
34+
make -C examples/async ASYNC_MODE=${{ matrix.async_mode }} EXTRA_CFLAGS="${{ matrix.extra_cflags }}"
3435
3536
- name: Run async examples
3637
run: |
3738
set -euo pipefail
3839
40+
ASYNC_MODE="${{ matrix.async_mode }}"
3941
MIN_PENDING=100
4042
4143
run_pair() {
@@ -63,16 +65,21 @@ jobs:
6365
return 1
6466
fi
6567
66-
# Validate WC_PENDING_E count is a proper value
67-
local count
68-
count=$(awk '/WC_PENDING_E count:/ {print $NF}' \
69-
"/tmp/async_client_${label}.log")
70-
if [ -z "$count" ] || [ "$count" -lt "$MIN_PENDING" ]; then
71-
echo "FAIL: $label - WC_PENDING_E count too low:" \
72-
"${count:-missing} (expected >= $MIN_PENDING)"
73-
return 1
68+
# Validate WC_PENDING_E count for sw mode only
69+
# cryptocb mode uses callback pending which isn't tracked the same way
70+
if [ "$ASYNC_MODE" = "sw" ]; then
71+
local count
72+
count=$(awk '/WC_PENDING_E count:/ {print $NF}' \
73+
"/tmp/async_client_${label}.log")
74+
if [ -z "$count" ] || [ "$count" -lt "$MIN_PENDING" ]; then
75+
echo "FAIL: $label - WC_PENDING_E count too low:" \
76+
"${count:-missing} (expected >= $MIN_PENDING)"
77+
return 1
78+
fi
79+
echo "PASS: $label (WC_PENDING_E: $count)"
80+
else
81+
echo "PASS: $label (cryptocb mode - connection successful)"
7482
fi
75-
echo "PASS: $label (WC_PENDING_E: $count)"
7683
return 0
7784
}
7885

examples/async/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ RM ?= rm -f
55
WOLFSSL_TOP ?= $(abspath ../..)
66
OBJDIR ?= build
77

8+
# Async mode: "sw" (default) for WOLFSSL_ASYNC_CRYPT_SW, "cryptocb" for WOLF_CRYPTO_CB
9+
ASYNC_MODE ?= sw
10+
811
CFLAGS ?= -O0 -g
912
CFLAGS += -I.
1013
CFLAGS += -I$(WOLFSSL_TOP)
@@ -14,6 +17,14 @@ CFLAGS += -Wall -Wextra -Wpedantic -Werror
1417
CFLAGS += -DWOLFSSL_USER_SETTINGS
1518
CFLAGS += -DHAVE_SYS_TIME_H
1619
CFLAGS += -DUSE_CERT_BUFFERS_256
20+
21+
# Set async mode defines based on ASYNC_MODE
22+
ifeq ($(ASYNC_MODE),cryptocb)
23+
CFLAGS += -DWOLF_CRYPTO_CB
24+
else
25+
CFLAGS += -DWOLFSSL_ASYNC_CRYPT_SW
26+
endif
27+
1728
CFLAGS += $(EXTRA_CFLAGS)
1829

1930
LDFLAGS ?=

examples/async/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,42 @@ Tested with:
1414
* `./configure --enable-asynccrypt --enable-pkcallbacks --enable-rsa --disable-ecc`
1515
* `./configure --enable-asynccrypt --enable-pkcallbacks --disable-rsa --enable-ecc`
1616

17+
## Build Modes
18+
19+
The async examples support two mutually exclusive async modes controlled via the
20+
`ASYNC_MODE` Makefile variable:
21+
22+
### Software Async Mode (default)
23+
Uses `WOLFSSL_ASYNC_CRYPT_SW` with non-blocking ECC (`WC_ECC_NONBLOCK`):
1724
```
1825
make -C examples/async
26+
# or explicitly:
27+
make -C examples/async ASYNC_MODE=sw
28+
```
29+
30+
### Crypto Callback Mode
31+
Uses `WOLF_CRYPTO_CB` with the `AsyncTlsCryptoCb` callback that simulates hardware
32+
crypto delays by returning `WC_PENDING_E` for a configurable number of iterations:
33+
```
34+
make -C examples/async ASYNC_MODE=cryptocb
35+
```
36+
37+
To adjust the simulated pending count (default is 2), define `TEST_PEND_COUNT`:
38+
```
39+
make -C examples/async ASYNC_MODE=cryptocb EXTRA_CFLAGS="-DTEST_PEND_COUNT=5"
40+
```
41+
42+
To enable crypto callback debug output:
43+
```
44+
make -C examples/async ASYNC_MODE=cryptocb EXTRA_CFLAGS="-DDEBUG_CRYPTOCB"
45+
```
46+
47+
**Note:** `WOLFSSL_ASYNC_CRYPT_SW` and `WOLF_CRYPTO_CB` are mutually exclusive in the
48+
async polling code (async.c uses `#elif`).
49+
50+
## Running the Examples
51+
52+
```
1953
./examples/async/async_server --ecc
2054
./examples/async/async_client --ecc 127.0.0.1 11111
2155
./examples/async/async_client --x25519 ecc256.badssl.com 443

examples/async/async_client.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
#include <wolfssl/ssl.h>
5353
#include <wolfssl/wolfio.h>
5454
#include <wolfssl/wolfcrypt/error-crypt.h>
55+
#ifdef WOLF_CRYPTO_CB
56+
#include <wolfssl/wolfcrypt/cryptocb.h>
57+
#endif
5558
#include <wolfssl/certs_test.h>
5659
#include "examples/async/async_tls.h"
5760

@@ -233,6 +236,9 @@ int client_async_test(int argc, char** argv)
233236
int wouldblock_count = 0;
234237
int pending_count = 0;
235238
#endif
239+
#ifdef WOLF_CRYPTO_CB
240+
AsyncTlsCryptoCbCtx cryptoCbCtx;
241+
#endif
236242
#ifdef WOLFSSL_STATIC_MEMORY
237243
static byte memory[300000];
238244
static byte memoryIO[34500];
@@ -279,6 +285,13 @@ int client_async_test(int argc, char** argv)
279285
goto out;
280286
}
281287
#endif
288+
#ifdef WOLF_CRYPTO_CB
289+
XMEMSET(&cryptoCbCtx, 0, sizeof(cryptoCbCtx));
290+
if (wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &cryptoCbCtx) != 0) {
291+
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed\n");
292+
goto out;
293+
}
294+
#endif
282295

283296
#ifdef WOLFSSL_STATIC_MEMORY
284297
{
@@ -548,6 +561,9 @@ int client_async_test(int argc, char** argv)
548561
if (ctx != NULL) {
549562
wolfSSL_CTX_free(ctx);
550563
}
564+
#ifdef WOLF_CRYPTO_CB
565+
wc_CryptoCb_UnRegisterDevice(devId);
566+
#endif
551567
#ifdef WOLFSSL_ASYNC_CRYPT
552568
if (devId != INVALID_DEVID) {
553569
wolfAsync_DevClose(&devId);

examples/async/async_server.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
#include <wolfssl/ssl.h>
5858
#include <wolfssl/wolfio.h>
5959
#include <wolfssl/wolfcrypt/error-crypt.h>
60+
#ifdef WOLF_CRYPTO_CB
61+
#include <wolfssl/wolfcrypt/cryptocb.h>
62+
#endif
6063
#include <wolfssl/certs_test.h>
6164
#include "examples/async/async_tls.h"
6265

@@ -191,6 +194,9 @@ int server_async_test(int argc, char** argv)
191194
int wouldblock_count = 0;
192195
int pending_count = 0;
193196
#endif
197+
#ifdef WOLF_CRYPTO_CB
198+
AsyncTlsCryptoCbCtx cryptoCbCtx;
199+
#endif
194200
#ifdef WOLFSSL_STATIC_MEMORY
195201
static byte memory[300000];
196202
static byte memoryIO[34500];
@@ -284,6 +290,13 @@ int server_async_test(int argc, char** argv)
284290
goto exit;
285291
}
286292
#endif
293+
#ifdef WOLF_CRYPTO_CB
294+
XMEMSET(&cryptoCbCtx, 0, sizeof(cryptoCbCtx));
295+
if (wc_CryptoCb_RegisterDevice(devId, AsyncTlsCryptoCb, &cryptoCbCtx) != 0) {
296+
fprintf(stderr, "ERROR: wc_CryptoCb_RegisterDevice failed\n");
297+
goto exit;
298+
}
299+
#endif
287300

288301
/* Create and initialize WOLFSSL_CTX */
289302
#ifdef WOLFSSL_STATIC_MEMORY
@@ -613,6 +626,9 @@ int server_async_test(int argc, char** argv)
613626
}
614627
if (ctx)
615628
wolfSSL_CTX_free(ctx);
629+
#ifdef WOLF_CRYPTO_CB
630+
wc_CryptoCb_UnRegisterDevice(devId);
631+
#endif
616632
#ifdef WOLFSSL_ASYNC_CRYPT
617633
if (devId != INVALID_DEVID) {
618634
wolfAsync_DevClose(&devId);

examples/async/async_tls.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
#include <config.h>
2424
#endif
2525

26-
#ifndef WOLFSSL_USER_SETTINGS
27-
#include <wolfssl/options.h>
26+
#ifdef WOLFSSL_USER_SETTINGS
27+
#include "user_settings.h"
28+
#else
29+
#include <wolfssl/options.h>
2830
#endif
2931
#include "examples/async/async_tls.h"
3032
#include <wolfssl/ssl.h>

examples/async/user_settings.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@
4545
#define WC_X25519_NONBLOCK
4646

4747
#define WOLFSSL_ASYNC_CRYPT
48-
#define WOLFSSL_ASYNC_CRYPT_SW
4948
#define WC_NO_ASYNC_THREADING
5049
#define HAVE_WOLF_BIGINT
5150

51+
/* Async mode is controlled via Makefile ASYNC_MODE variable:
52+
* - ASYNC_MODE=sw (default): WOLFSSL_ASYNC_CRYPT_SW for non-blocking ECC
53+
* - ASYNC_MODE=cryptocb: WOLF_CRYPTO_CB for crypto callback simulation
54+
* These are mutually exclusive in async.c polling code (#elif).
55+
* See README.md for build instructions.
56+
*/
57+
5258
#define HAVE_AESGCM
5359

5460
#define WOLFSSL_SHA512
61+
#define WOLFSSL_HASH_FLAGS
5562

5663
#define WOLFSSL_TLS13
5764
#define HAVE_HKDF

0 commit comments

Comments
 (0)