Skip to content

Commit a029d20

Browse files
committed
Improvements to increase number of iterations for each.
1 parent ae6dc4c commit a029d20

7 files changed

Lines changed: 290 additions & 36 deletions

File tree

.github/workflows/os-check.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ jobs:
8181
'--enable-all CPPFLAGS=-DWOLFSSL_NO_CLIENT_AUTH',
8282
'--enable-all CPPFLAGS=''-DNO_WOLFSSL_CLIENT -DWOLFSSL_NO_CLIENT_AUTH''',
8383
'--enable-all CPPFLAGS=''-DNO_WOLFSSL_SERVER -DWOLFSSL_NO_CLIENT_AUTH''',
84+
'--enable-curve25519=nonblock --enable-ecc=nonblock --enable-sp=yes,nonblock --disable-examples CFLAGS="-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"',
8485
]
8586
name: make check
8687
if: github.repository_owner == 'wolfssl'
@@ -124,6 +125,7 @@ jobs:
124125
user-settings: [
125126
# Add new user_settings.h here
126127
'examples/configs/user_settings_eccnonblock.h',
128+
'examples/configs/user_settings_curve25519nonblock.h',
127129
'examples/configs/user_settings_min_ecc.h',
128130
'examples/configs/user_settings_wolfboot_keytools.h',
129131
'examples/configs/user_settings_wolftpm.h',

examples/configs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Example wolfSSL configuration file templates for use when autoconf is not availa
1010
*.`user_settings_EBSnet.h`: Example configuration file for use with EBSnet ports.
1111
* `user_settings_fipsv2.h`: The FIPS v2 (3389) 140-2 certificate build options.
1212
* `user_settings_fipsv5.h`: The FIPS v5 (ready) 140-3 build options. Equivalent to `./configure --enable-fips=v5-dev`.
13+
* `user_settings_curve25519nonblock.h`: Example Curve25519 (X25519) non-blocking configuration.
1314
* `user_settings_min_ecc.h`: This is ECC and SHA-256 only. For ECC verify only add `BUILD_VERIFY_ONLY`.
1415
* `user_settings_platformio.h`: An example for PlatformIO library. See also [platformio/wolfssl](https://registry.platformio.org/libraries/wolfssl/wolfssl)
1516
* `user_settings_stm32.h`: Example configuration file generated from the wolfSSL STM32 Cube pack.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* user_settings_curve25519nonblock.h
2+
*
3+
* Copyright (C) 2006-2025 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+
22+
/* Example wolfSSL user_settings.h file for Curve25519 (X25519) non-blocking.
23+
* See doc/dox_comments/header_files/curve25519.h wc_curve25519_set_nonblock.
24+
*/
25+
26+
/* Settings based on this configure:
27+
./configure --enable-curve25519=nonblock --enable-ecc=nonblock \
28+
--enable-sp=yes,nonblock \
29+
CFLAGS="-DWOLFSSL_PUBLIC_MP -DWOLFSSL_DEBUG_NONBLOCK"
30+
*/
31+
32+
/* Tested using:
33+
cp ./examples/configs/user_settings_curve25519nonblock.h user_settings.h
34+
./configure --enable-usersettings --enable-debug --disable-examples
35+
make
36+
./wolfcrypt/test/testwolfcrypt
37+
*/
38+
39+
/* Example test results:
40+
CURVE25519 non-block key gen: 1273 times
41+
CURVE25519 non-block shared secret: 1275 times
42+
CURVE25519 test passed!
43+
*/
44+
45+
#ifndef WOLFSSL_USER_SETTINGS_H
46+
#define WOLFSSL_USER_SETTINGS_H
47+
48+
#ifdef __cplusplus
49+
extern "C" {
50+
#endif
51+
52+
/* Features */
53+
#define WOLFCRYPT_ONLY
54+
#define WOLFSSL_ASN_TEMPLATE
55+
#define WOLFSSL_PUBLIC_MP /* expose mp_ math API's */
56+
#define HAVE_HASHDRBG
57+
58+
/* Curve25519 (X25519) */
59+
#define HAVE_CURVE25519
60+
#define CURVE25519_SMALL
61+
#define WC_X25519_NONBLOCK
62+
63+
/* Debugging */
64+
#if 1
65+
#undef DEBUG_WOLFSSL
66+
#define DEBUG_WOLFSSL
67+
#define WOLFSSL_DEBUG_NONBLOCK
68+
#endif
69+
70+
/* Disabled algorithms */
71+
#define NO_OLD_TLS
72+
#define NO_RSA
73+
#define NO_DH
74+
#define NO_PSK
75+
#define NO_MD4
76+
#define NO_MD5
77+
#define NO_SHA
78+
#define NO_DSA
79+
#define NO_DES3
80+
#define NO_RC4
81+
#define WOLFSSL_NO_SHAKE128
82+
#define WOLFSSL_NO_SHAKE256
83+
84+
#ifdef __cplusplus
85+
}
86+
#endif
87+
88+
#endif /* WOLFSSL_USER_SETTINGS_H */

wolfcrypt/src/curve25519.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
#endif
7070
#endif
7171

72+
#if defined(WOLFSSL_CURVE25519_BLINDING)
73+
WOLFSSL_LOCAL int curve25519_blind(byte* q, const byte* n, const byte* mask,
74+
const byte* p, const byte* rz);
75+
#endif
76+
7277
#if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && !defined(USE_INTEL_SPEEDUP)
7378
/* force off unneeded vector register save/restore. */
7479
#undef SAVE_VECTOR_REGISTERS

wolfcrypt/src/fe_low_mem.c

Lines changed: 173 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -191,27 +191,64 @@ int fe_inv__distinct_nb(byte *r, const byte *x, fe_inv__distinct_nb_ctx_t* ctx)
191191
fe_mul__distinct(ctx->s, x, x);
192192
fe_mul__distinct(r, ctx->s, x);
193193
ctx->i = 0;
194+
ctx->subState = 0;
194195
ctx->state = 1;
195196
break;
196197
case 1:
197-
if ((ctx->i)++ < 248) {
198-
fe_mul__distinct(ctx->s, r, r);
199-
fe_mul__distinct(r, ctx->s, x);
198+
if (ctx->i < 248) {
199+
if (ctx->subState == 0) {
200+
fe_mul__distinct(ctx->s, r, r);
201+
ctx->subState = 1;
202+
}
203+
else {
204+
fe_mul__distinct(r, ctx->s, x);
205+
ctx->subState = 0;
206+
++(ctx->i);
207+
}
200208
}
201209
else {
202210
ctx->state = 2;
211+
ctx->subState = 0;
203212
}
204213
break;
205214
case 2:
206-
fe_mul__distinct(ctx->s, r, r);
207-
fe_mul__distinct(r, ctx->s, ctx->s);
208-
fe_mul__distinct(ctx->s, r, x);
209-
fe_mul__distinct(r, ctx->s, ctx->s);
210-
fe_mul__distinct(ctx->s, r, r);
211-
fe_mul__distinct(r, ctx->s, x);
212-
fe_mul__distinct(ctx->s, r, r);
213-
fe_mul__distinct(r, ctx->s, x);
214-
ret = 0;
215+
switch (ctx->subState) {
216+
case 0:
217+
fe_mul__distinct(ctx->s, r, r);
218+
ctx->subState = 1;
219+
break;
220+
case 1:
221+
fe_mul__distinct(r, ctx->s, ctx->s);
222+
ctx->subState = 2;
223+
break;
224+
case 2:
225+
fe_mul__distinct(ctx->s, r, x);
226+
ctx->subState = 3;
227+
break;
228+
case 3:
229+
fe_mul__distinct(r, ctx->s, ctx->s);
230+
ctx->subState = 4;
231+
break;
232+
case 4:
233+
fe_mul__distinct(ctx->s, r, r);
234+
ctx->subState = 5;
235+
break;
236+
case 5:
237+
fe_mul__distinct(r, ctx->s, x);
238+
ctx->subState = 6;
239+
break;
240+
case 6:
241+
fe_mul__distinct(ctx->s, r, r);
242+
ctx->subState = 7;
243+
break;
244+
case 7:
245+
fe_mul__distinct(r, ctx->s, x);
246+
ret = 0;
247+
break;
248+
default:
249+
ctx->subState = 0;
250+
break;
251+
}
215252
break;
216253
}
217254

@@ -237,33 +274,134 @@ int curve25519_nb(byte *result, const byte *n, const byte *p,
237274
XMEMSET(ctx->zm1, 0, sizeof(ctx->zm1));
238275
lm_copy(ctx->xm, p);
239276
ctx->i = 253;
277+
ctx->subState = 0;
240278
ctx->state = 1;
241279
break;
242280
case 1:
243281
if (ctx->i >= 0) {
244-
const int bit = (n[ctx->i >> 3] >> (ctx->i & 7)) & 1;
245-
byte xms[F25519_SIZE];
246-
byte zms[F25519_SIZE];
247-
248-
/* From P_m and P_(m-1), compute P_(2m) and P_(2m-1) */
249-
xc_diffadd(ctx->xm1, ctx->zm1, p, f25519_one, ctx->xm, ctx->zm,
250-
ctx->xm1, ctx->zm1);
251-
xc_double(ctx->xm, ctx->zm, ctx->xm, ctx->zm);
252-
253-
/* Compute P_(2m+1) */
254-
xc_diffadd(xms, zms, ctx->xm1, ctx->zm1, ctx->xm,
255-
ctx->zm, p, f25519_one);
256-
257-
/* Select:
258-
* bit = 1 --> (P_(2m+1), P_(2m))
259-
* bit = 0 --> (P_(2m), P_(2m-1))
260-
*/
261-
fe_select(ctx->xm1, ctx->xm1, ctx->xm, bit);
262-
fe_select(ctx->zm1, ctx->zm1, ctx->zm, bit);
263-
fe_select(ctx->xm, ctx->xm, xms, bit);
264-
fe_select(ctx->zm, ctx->zm, zms, bit);
265-
266-
--(ctx->i);
282+
switch (ctx->subState) {
283+
case 0:
284+
ctx->bit = (n[ctx->i >> 3] >> (ctx->i & 7)) & 1;
285+
/* Diffadd step 1 */
286+
lm_add(ctx->a, ctx->xm, ctx->zm);
287+
lm_sub(ctx->b, ctx->xm1, ctx->zm1);
288+
fe_mul__distinct(ctx->da, ctx->a, ctx->b);
289+
ctx->subState = 1;
290+
break;
291+
case 1:
292+
/* Diffadd step 2 */
293+
lm_sub(ctx->b, ctx->xm, ctx->zm);
294+
lm_add(ctx->a, ctx->xm1, ctx->zm1);
295+
fe_mul__distinct(ctx->cb, ctx->a, ctx->b);
296+
ctx->subState = 2;
297+
break;
298+
case 2:
299+
/* Diffadd step 3 */
300+
lm_add(ctx->a, ctx->da, ctx->cb);
301+
fe_mul__distinct(ctx->b, ctx->a, ctx->a);
302+
ctx->subState = 3;
303+
break;
304+
case 3:
305+
/* Diffadd step 4 */
306+
fe_mul__distinct(ctx->xm1, f25519_one, ctx->b);
307+
ctx->subState = 4;
308+
break;
309+
case 4:
310+
/* Diffadd step 5 */
311+
lm_sub(ctx->a, ctx->da, ctx->cb);
312+
fe_mul__distinct(ctx->b, ctx->a, ctx->a);
313+
ctx->subState = 5;
314+
break;
315+
case 5:
316+
/* Diffadd step 6 */
317+
fe_mul__distinct(ctx->zm1, p, ctx->b);
318+
ctx->subState = 6;
319+
break;
320+
case 6:
321+
/* Double step 1 */
322+
fe_mul__distinct(ctx->x1sq, ctx->xm, ctx->xm);
323+
ctx->subState = 7;
324+
break;
325+
case 7:
326+
/* Double step 2 */
327+
fe_mul__distinct(ctx->z1sq, ctx->zm, ctx->zm);
328+
ctx->subState = 8;
329+
break;
330+
case 8:
331+
/* Double step 3 */
332+
fe_mul__distinct(ctx->x1z1, ctx->xm, ctx->zm);
333+
ctx->subState = 9;
334+
break;
335+
case 9:
336+
/* Double step 4 */
337+
lm_sub(ctx->a, ctx->x1sq, ctx->z1sq);
338+
fe_mul__distinct(ctx->xm, ctx->a, ctx->a);
339+
ctx->subState = 10;
340+
break;
341+
case 10:
342+
/* Double step 5 */
343+
fe_mul_c(ctx->a, ctx->x1z1, 486662);
344+
lm_add(ctx->a, ctx->x1sq, ctx->a);
345+
lm_add(ctx->a, ctx->z1sq, ctx->a);
346+
fe_mul__distinct(ctx->x1sq, ctx->x1z1, ctx->a);
347+
ctx->subState = 11;
348+
break;
349+
case 11:
350+
fe_mul_c(ctx->zm, ctx->x1sq, 4);
351+
ctx->subState = 12;
352+
break;
353+
case 12:
354+
/* Diffadd2 step 1 */
355+
lm_add(ctx->a, ctx->xm, ctx->zm);
356+
lm_sub(ctx->b, p, f25519_one);
357+
fe_mul__distinct(ctx->da, ctx->a, ctx->b);
358+
ctx->subState = 13;
359+
break;
360+
case 13:
361+
/* Diffadd2 step 2 */
362+
lm_sub(ctx->b, ctx->xm, ctx->zm);
363+
lm_add(ctx->a, p, f25519_one);
364+
fe_mul__distinct(ctx->cb, ctx->a, ctx->b);
365+
ctx->subState = 14;
366+
break;
367+
case 14:
368+
/* Diffadd2 step 3 */
369+
lm_add(ctx->a, ctx->da, ctx->cb);
370+
fe_mul__distinct(ctx->b, ctx->a, ctx->a);
371+
ctx->subState = 15;
372+
break;
373+
case 15:
374+
/* Diffadd2 step 4 */
375+
fe_mul__distinct(ctx->xms, ctx->zm1, ctx->b);
376+
ctx->subState = 16;
377+
break;
378+
case 16:
379+
/* Diffadd2 step 5 */
380+
lm_sub(ctx->a, ctx->da, ctx->cb);
381+
fe_mul__distinct(ctx->b, ctx->a, ctx->a);
382+
ctx->subState = 17;
383+
break;
384+
case 17:
385+
/* Diffadd2 step 6 */
386+
fe_mul__distinct(ctx->zms, ctx->xm1, ctx->b);
387+
ctx->subState = 18;
388+
break;
389+
case 18:
390+
/* Select:
391+
* bit = 1 --> (P_(2m+1), P_(2m))
392+
* bit = 0 --> (P_(2m), P_(2m-1))
393+
*/
394+
fe_select(ctx->xm1, ctx->xm1, ctx->xm, ctx->bit);
395+
fe_select(ctx->zm1, ctx->zm1, ctx->zm, ctx->bit);
396+
fe_select(ctx->xm, ctx->xm, ctx->xms, ctx->bit);
397+
fe_select(ctx->zm, ctx->zm, ctx->zms, ctx->bit);
398+
--(ctx->i);
399+
ctx->subState = 0;
400+
break;
401+
default:
402+
ctx->subState = 0;
403+
break;
404+
}
267405
}
268406
else {
269407
ctx->state = 2;

wolfcrypt/test/test.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38586,7 +38586,11 @@ static wc_test_ret_t curve255519_der_test(void)
3858638586
#endif /* !NO_ASN && HAVE_CURVE25519_KEY_EXPORT && HAVE_CURVE25519_KEY_IMPORT */
3858738587

3858838588
#ifdef WC_X25519_NONBLOCK
38589-
38589+
/* build and test with:
38590+
* ./configure --enable-curve25519=nonblock CFLAGS="-DWOLFSSL_DEBUG_NONBLOCK"
38591+
* make
38592+
* ./wolfcrypt/test/testwolfcrypt
38593+
*/
3859038594
static int x25519_nonblock_test(WC_RNG* rng)
3859138595
{
3859238596
int ret = 0;
@@ -38623,6 +38627,7 @@ static int x25519_nonblock_test(WC_RNG* rng)
3862338627
return -10724;
3862438628
}
3862538629
#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
38630+
/* CURVE25519 non-block key gen: 5335 times */
3862638631
printf("CURVE25519 non-block key gen: %d times\n", count);
3862738632
#endif
3862838633

@@ -38669,6 +38674,7 @@ static int x25519_nonblock_test(WC_RNG* rng)
3866938674
return -10728;
3867038675
}
3867138676
#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
38677+
/* CURVE25519 non-block shared secret: 5337 times */
3867238678
printf("CURVE25519 non-block shared secret: %d times\n", count);
3867338679
#endif
3867438680

0 commit comments

Comments
 (0)