Merge pull request #9721 from dgarske/x25519_nb

Add X25519 non-blocking support and async example improvements
This commit is contained in:
Sean Parkinson
2026-02-12 07:56:58 +10:00
committed by GitHub
39 changed files with 2727 additions and 564 deletions
+12 -12
View File
@@ -6851,14 +6851,14 @@ static const unsigned char server_ed25519_cert[] =
};
#define sizeof_server_ed25519_cert (sizeof(server_ed25519_cert))
/* ./certs/ed25519/server-ed25519-key.der, ED25519 */
/* ./certs/ed25519/server-ed25519-priv.der, ED25519 */
static const unsigned char server_ed25519_key[] =
{
0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03,
0x21, 0x00, 0x23, 0xAA, 0x4D, 0x60, 0x50, 0xE0, 0x13, 0xD3,
0x3A, 0xED, 0xAB, 0xF6, 0xA9, 0xCC, 0x4A, 0xFE, 0xD7, 0x4D,
0x2F, 0xD2, 0x5B, 0x1A, 0x10, 0x05, 0xEF, 0x5A, 0x41, 0x25,
0xCE, 0x1B, 0x53, 0x78
0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B,
0x65, 0x70, 0x04, 0x22, 0x04, 0x20, 0x8E, 0x98, 0x44, 0xB0,
0x54, 0x81, 0xC6, 0x3A, 0x47, 0xD8, 0xFB, 0xC3, 0x36, 0xBF,
0x19, 0x70, 0x61, 0x09, 0x23, 0x76, 0xE3, 0x1C, 0x6F, 0x83,
0x38, 0xAE, 0x49, 0x55, 0xC5, 0x9E, 0x87, 0x22
};
#define sizeof_server_ed25519_key (sizeof(server_ed25519_key))
@@ -7030,14 +7030,14 @@ static const unsigned char client_ed25519_cert[] =
};
#define sizeof_client_ed25519_cert (sizeof(client_ed25519_cert))
/* ./certs/ed25519/client-ed25519-key.der, ED25519 */
/* ./certs/ed25519/client-ed25519-priv.der, ED25519 */
static const unsigned char client_ed25519_key[] =
{
0x30, 0x2A, 0x30, 0x05, 0x06, 0x03, 0x2B, 0x65, 0x70, 0x03,
0x21, 0x00, 0xE6, 0x57, 0x5B, 0x13, 0x1B, 0xC7, 0x51, 0x14,
0x6B, 0xED, 0x3B, 0xF5, 0xD1, 0xFA, 0xAB, 0x9E, 0x6C, 0xB6,
0xEB, 0x02, 0x09, 0xA3, 0x99, 0xF5, 0x6E, 0xBF, 0x9D, 0x3C,
0xFE, 0x54, 0x39, 0xE6
0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2B,
0x65, 0x70, 0x04, 0x22, 0x04, 0x20, 0x92, 0xB5, 0x4C, 0xEC,
0xAF, 0x81, 0xC6, 0xBB, 0x01, 0xD6, 0xD5, 0xDE, 0xBD, 0x37,
0x97, 0x5A, 0xD2, 0xC6, 0xF6, 0xC3, 0x85, 0xB5, 0x3B, 0xE6,
0xE4, 0xEC, 0x32, 0xE9, 0xC7, 0xCA, 0x52, 0xEB
};
#define sizeof_client_ed25519_key (sizeof(client_ed25519_key))
+27
View File
@@ -81,6 +81,10 @@ struct WC_ASYNC_DEV;
ASYNC_SW_DES3_CBC_ENCRYPT = 13,
ASYNC_SW_DES3_CBC_DECRYPT = 14,
#endif /* !NO_DES3 */
#ifdef HAVE_CURVE25519
ASYNC_SW_X25519_MAKE = 15,
ASYNC_SW_X25519_SHARED_SEC = 16,
#endif /* HAVE_CURVE25519 */
};
#ifdef HAVE_ECC
@@ -179,6 +183,21 @@ struct WC_ASYNC_DEV;
};
#endif /* !NO_DES3 */
#ifdef HAVE_CURVE25519
struct AsyncCryptX25519Make {
void* rng; /* WC_RNG */
void* key; /* curve25519_key */
int size;
};
struct AsyncCryptX25519SharedSec {
void* priv; /* curve25519_key */
void* pub; /* curve25519_key */
byte* out;
word32* outLen;
int endian;
};
#endif /* HAVE_CURVE25519 */
#ifdef __CC_ARM
#pragma push
#pragma anon_unions
@@ -211,6 +230,10 @@ struct WC_ASYNC_DEV;
#ifndef NO_DES3
struct AsyncCryptSwDes des;
#endif /* !NO_DES3 */
#ifdef HAVE_CURVE25519
struct AsyncCryptX25519Make x25519Make;
struct AsyncCryptX25519SharedSec x25519SharedSec;
#endif /* HAVE_CURVE25519 */
#ifdef HAVE_ANONYMOUS_INLINE_AGGREGATES
}; /* union */
#endif
@@ -288,6 +311,9 @@ struct WC_ASYNC_DEV;
#ifndef WC_ASYNC_NO_DH
#define WC_ASYNC_ENABLE_DH
#endif
#ifndef WC_ASYNC_NO_X25519
#define WC_ASYNC_ENABLE_X25519
#endif
#endif /* WC_ASYNC_NO_PKI */
#ifndef WC_ASYNC_NO_HASH
#ifndef WC_ASYNC_NO_SHA512
@@ -337,6 +363,7 @@ struct WC_ASYNC_DEV;
#define WOLFSSL_ASYNC_MARKER_MD5 0xBEEF000D
#define WOLFSSL_ASYNC_MARKER_DH 0xBEEF000E
#define WOLFSSL_ASYNC_MARKER_SHA3 0xBEEF000F
#define WOLFSSL_ASYNC_MARKER_X25519 0xBEEF0010
/* event flags (bit mask) */
+64
View File
@@ -74,6 +74,46 @@ typedef struct ECPoint {
#define WC_CURVE25519KEY_TYPE_DEFINED
#endif
#ifdef WC_X25519_NONBLOCK
typedef struct fe_inv__distinct_nb_ctx_t {
int state;
int subState;
byte s[F25519_SIZE];
int i;
} fe_inv__distinct_nb_ctx_t;
typedef struct x25519_nb_ctx_t {
/* state for curve25519 operation */
int state;
int subState;
/* state for shared secret */
int ssState;
int i;
int bit;
/* Current point: P_m */
byte xm[F25519_SIZE];
byte zm[F25519_SIZE];
/* Predecessor: P_(m-1) */
byte xm1[F25519_SIZE];
byte zm1[F25519_SIZE];
/* Temporary P_(2m+1) */
byte xms[F25519_SIZE];
byte zms[F25519_SIZE];
/* Temporary buffers for non-blocking diffadd/double */
byte a[F25519_SIZE];
byte b[F25519_SIZE];
byte da[F25519_SIZE];
byte cb[F25519_SIZE];
byte x1sq[F25519_SIZE];
byte z1sq[F25519_SIZE];
byte x1z1[F25519_SIZE];
fe_inv__distinct_nb_ctx_t inv_distinct_nb_ctx;
ECPoint o; /* point for shared secret */
} x25519_nb_ctx_t;
#endif /* WC_X25519_NONBLOCK */
/* A CURVE25519 Key */
struct curve25519_key {
int idx; /* Index into the ecc_sets[] for the parameters of
@@ -100,6 +140,10 @@ struct curve25519_key {
byte keyIdSet;
#endif
#ifdef WC_X25519_NONBLOCK
x25519_nb_ctx_t* nb_ctx;
#endif /* WC_X25519_NONBLOCK */
/* bit fields */
WC_BITFIELD pubSet:1;
WC_BITFIELD privSet:1;
@@ -137,6 +181,26 @@ int wc_curve25519_make_priv(WC_RNG* rng, int keysize, byte* priv);
WOLFSSL_API
int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key);
#ifdef WC_X25519_NONBLOCK
/*!
\brief Enable non-blocking support for X25519 operations on a key.
When enabled, wc_curve25519_make_key() and
wc_curve25519_shared_secret() will return FP_WOULDBLOCK during
operation, allowing the caller to
yield and resume. Requires WC_X25519_NONBLOCK and CURVE25519_SMALL.
\param key Pointer to curve25519_key structure to configure
\param ctx Pointer to x25519_nb_ctx_t context for state tracking,
or NULL to disable non-blocking mode
\return 0 on success, BAD_FUNC_ARG if key is NULL
*/
WOLFSSL_API
int wc_curve25519_set_nonblock(curve25519_key* key, x25519_nb_ctx_t* ctx);
#endif /* WC_X25519_NONBLOCK */
WOLFSSL_API
int wc_curve25519_shared_secret(curve25519_key* private_key,
curve25519_key* public_key,
+38 -11
View File
@@ -68,25 +68,52 @@ Bounds on each t[i] vary depending on context.
#endif
#if defined(CURVE25519_SMALL) || defined(ED25519_SMALL)
#define F25519_SIZE 32
WOLFSSL_LOCAL void lm_copy(byte* x, const byte* a);
WOLFSSL_LOCAL void lm_add(byte* r, const byte* a, const byte* b);
WOLFSSL_LOCAL void lm_sub(byte* r, const byte* a, const byte* b);
WOLFSSL_LOCAL void lm_neg(byte* r,const byte* a);
WOLFSSL_LOCAL void lm_invert(byte* r, const byte* x);
WOLFSSL_LOCAL void lm_mul(byte* r,const byte* a, const byte* b);
#define F25519_SIZE 32
#include <wolfssl/wolfcrypt/curve25519.h>
WOLFSSL_LOCAL void lm_copy(byte*, const byte*);
WOLFSSL_LOCAL void lm_add(byte*, const byte*, const byte*);
WOLFSSL_LOCAL void lm_sub(byte*, const byte*, const byte*);
WOLFSSL_LOCAL void lm_neg(byte*,const byte*);
WOLFSSL_LOCAL void lm_invert(byte*, const byte*);
WOLFSSL_LOCAL void lm_mul(byte*,const byte*,const byte*);
#ifdef WC_X25519_NONBLOCK
/* Use standard wolfSSL non-blocking error code */
#ifndef FP_WOULDBLOCK
#include <wolfssl/wolfcrypt/error-crypt.h>
#define FP_WOULDBLOCK MP_WOULDBLOCK
#endif
struct fe_inv__distinct_nb_ctx_t;
struct x25519_nb_ctx_t;
WOLFSSL_LOCAL int fe_inv__distinct_nb(byte *r, const byte *x,
struct fe_inv__distinct_nb_ctx_t* ctx);
WOLFSSL_LOCAL int curve25519_nb(byte * q, const byte * n, const byte * p,
struct x25519_nb_ctx_t* ctx);
#endif /* WC_X25519_NONBLOCK */
#else
#ifdef WC_X25519_NONBLOCK
#error The X25519 non-blocking requires CURVE25519_SMALL \
(--enable-curve25519=small)
#endif
#endif /* CURVE25519_SMALL || ED25519_SMALL */
#if !defined(FREESCALE_LTC_ECC)
WOLFSSL_LOCAL void fe_init(void);
WOLFSSL_LOCAL int curve25519_base(byte * q, const byte * n);
WOLFSSL_LOCAL int curve25519(byte * q, const byte * n, const byte * p);
WOLFSSL_LOCAL int curve25519(byte * q, const byte * n, const byte * p);
#ifdef WOLFSSL_CURVE25519_BLINDING
WOLFSSL_LOCAL int curve25519_blind(byte * q, const byte * n, const byte* mask,
const byte * p, const byte* rz);
WOLFSSL_LOCAL int curve25519_blind(byte* q, const byte* n, const byte* mask,
const byte* p, const byte* rz);
#endif
#endif