Skip to content

Commit ae6dc4c

Browse files
committed
Support for Curve25519 non-blocking cryptography (based on PR #5764)
1 parent a631611 commit ae6dc4c

7 files changed

Lines changed: 682 additions & 102 deletions

File tree

configure.ac

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,11 +4816,18 @@ ENABLED_ED25519_SMALL=no
48164816

48174817
# CURVE25519
48184818
AC_ARG_ENABLE([curve25519],
4819-
[AS_HELP_STRING([--enable-curve25519],[Enable Curve25519 (default: disabled)])],
4819+
[AS_HELP_STRING([--enable-curve25519],[Enable Curve25519 (default: disabled). Set to "nonblock" to enable non-blocking support for key gen and shared secret])],
48204820
[ ENABLED_CURVE25519=$enableval ],
48214821
[ ENABLED_CURVE25519=no ]
48224822
)
48234823

4824+
# Handle curve25519 nonblock option - enable asynccrypt and asynccrypt-sw early
4825+
if test "$ENABLED_CURVE25519" = "nonblock"
4826+
then
4827+
test -z "$enable_asynccrypt" && enable_asynccrypt=yes
4828+
test -z "$enable_asynccrypt_sw" && enable_asynccrypt_sw=yes
4829+
fi
4830+
48244831
if test "$ENABLED_CURVE25519" = "no" && test "$ENABLED_QUIC" = "yes" && test "$ENABLED_FIPS" = "no"
48254832
then
48264833
ENABLED_CURVE25519=yes
@@ -10328,12 +10335,17 @@ fi
1032810335
1032910336
if test "$ENABLED_CURVE25519" != "no"
1033010337
then
10331-
if test "$ENABLED_CURVE25519" = "small" || test "$ENABLED_LOWRESOURCE" = "yes"
10338+
if test "$ENABLED_CURVE25519" = "small" || test "$ENABLED_CURVE25519" = "nonblock" || test "$ENABLED_LOWRESOURCE" = "yes"
1033210339
then
1033310340
AM_CFLAGS="$AM_CFLAGS -DCURVE25519_SMALL"
1033410341
ENABLED_CURVE25519_SMALL=yes
1033510342
fi
1033610343
10344+
if test "$ENABLED_CURVE25519" = "nonblock"
10345+
then
10346+
AM_CFLAGS="$AM_CFLAGS -DWC_X25519_NONBLOCK"
10347+
fi
10348+
1033710349
if test "$ENABLED_CURVE25519" = "no128bit" || test "$ENABLED_32BIT" = "yes"
1033810350
then
1033910351
AM_CFLAGS="$AM_CFLAGS -DNO_CURVED25519_128BIT"

src/internal.c

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6117,7 +6117,7 @@ static int X25519SharedSecret(WOLFSSL* ssl, curve25519_key* priv_key,
61176117

61186118
#ifdef WOLFSSL_ASYNC_CRYPT
61196119
/* initialize event */
6120-
ret = wolfSSL_AsyncInit(ssl, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
6120+
ret = wolfSSL_AsyncInit(ssl, &priv_key->asyncDev, WC_ASYNC_FLAG_NONE);
61216121
if (ret != 0)
61226122
return ret;
61236123
#endif
@@ -8189,6 +8189,13 @@ void FreeKey(WOLFSSL* ssl, int type, void** pKey)
81898189
#endif /* HAVE_ED25519 */
81908190
#ifdef HAVE_CURVE25519
81918191
case DYNAMIC_TYPE_CURVE25519:
8192+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8193+
defined(WC_ASYNC_ENABLE_X25519)
8194+
if (((curve25519_key*)*pKey)->nbCtx != NULL) {
8195+
XFREE(((curve25519_key*)*pKey)->nbCtx, ssl->heap,
8196+
DYNAMIC_TYPE_TMP_BUFFER);
8197+
}
8198+
#endif
81928199
wc_curve25519_free((curve25519_key*)*pKey);
81938200
break;
81948201
#endif /* HAVE_CURVE25519 */
@@ -8236,8 +8243,15 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
82368243
#endif /* HAVE_ECC */
82378244
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
82388245
defined(WC_ASYNC_ENABLE_ECC)
8239-
ecc_nb_ctx_t* nbCtx;
8240-
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW && WC_ASYNC_ENABLE_ECC*/
8246+
ecc_nb_ctx_t* eccNbCtx;
8247+
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW && WC_ASYNC_ENABLE_ECC */
8248+
#ifdef HAVE_CURVE25519
8249+
curve25519_key* x25519Key;
8250+
#endif /* HAVE_CURVE25519 */
8251+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8252+
defined(WC_ASYNC_ENABLE_X25519)
8253+
x25519_nb_ctx_t* x25519NbCtx;
8254+
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW && WC_ASYNC_ENABLE_X25519 */
82418255

82428256
if (ssl == NULL || pKey == NULL) {
82438257
return BAD_FUNC_ARG;
@@ -8323,23 +8337,23 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83238337
case DYNAMIC_TYPE_ECC:
83248338
eccKey = (ecc_key*)*pKey;
83258339
ret = wc_ecc_init_ex(eccKey, ssl->heap, ssl->devId);
8340+
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8341+
defined(WC_ASYNC_ENABLE_ECC)
83268342
if (ret == 0) {
8327-
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8328-
defined(WC_ASYNC_ENABLE_ECC)
8329-
nbCtx = (ecc_nb_ctx_t*)XMALLOC(sizeof(ecc_nb_ctx_t),
8330-
eccKey->heap, DYNAMIC_TYPE_TMP_BUFFER);
8331-
if (nbCtx == NULL) {
8343+
eccNbCtx = (ecc_nb_ctx_t*)XMALLOC(sizeof(ecc_nb_ctx_t),
8344+
eccKey->heap, DYNAMIC_TYPE_TMP_BUFFER);
8345+
if (eccNbCtx == NULL) {
83328346
ret = MEMORY_E;
83338347
}
83348348
else {
8335-
ret = wc_ecc_set_nonblock(eccKey, nbCtx);
8349+
ret = wc_ecc_set_nonblock(eccKey, eccNbCtx);
83368350
if (ret != 0) {
8337-
XFREE(nbCtx, eccKey->heap, DYNAMIC_TYPE_TMP_BUFFER);
8351+
XFREE(eccNbCtx, eccKey->heap, DYNAMIC_TYPE_TMP_BUFFER);
83388352
}
83398353
}
8340-
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8341-
WC_ASYNC_ENABLE_ECC */
83428354
}
8355+
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8356+
WC_ASYNC_ENABLE_ECC */
83438357
break;
83448358
#endif /* HAVE_ECC */
83458359
#ifdef HAVE_ED25519
@@ -8350,8 +8364,25 @@ int AllocKey(WOLFSSL* ssl, int type, void** pKey)
83508364
#endif /* HAVE_CURVE25519 */
83518365
#ifdef HAVE_CURVE25519
83528366
case DYNAMIC_TYPE_CURVE25519:
8353-
wc_curve25519_init_ex((curve25519_key*)*pKey, ssl->heap, ssl->devId);
8354-
ret = 0;
8367+
x25519Key = (curve25519_key*)*pKey;
8368+
ret = wc_curve25519_init_ex(x25519Key, ssl->heap, ssl->devId);
8369+
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8370+
defined(WC_ASYNC_ENABLE_X25519)
8371+
if (ret == 0) {
8372+
x25519NbCtx = (x25519_nb_ctx_t*)XMALLOC(sizeof(x25519_nb_ctx_t),
8373+
ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8374+
if (x25519NbCtx == NULL) {
8375+
ret = MEMORY_E;
8376+
}
8377+
else {
8378+
ret = wc_curve25519_set_nonblock(x25519Key, x25519NbCtx);
8379+
if (ret != 0) {
8380+
XFREE(x25519NbCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8381+
}
8382+
}
8383+
}
8384+
#endif /* WC_X25519_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8385+
WC_ASYNC_ENABLE_X25519 */
83558386
break;
83568387
#endif /* HAVE_CURVE25519 */
83578388
#ifdef HAVE_ED448

0 commit comments

Comments
 (0)