Skip to content

Commit 1c82403

Browse files
committed
Curve25519 needs an RNG for blinding
1 parent 089f8a9 commit 1c82403

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

pk/curve25519/curve25519_test.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* curve25519_test.c
22
*
3-
* Copyright (C) 2006-2021 wolfSSL Inc.
3+
* Copyright (C) 2006-2025 wolfSSL Inc.
44
*
55
* This file is part of wolfSSL. (formerly known as CyaSSL)
66
*
@@ -25,12 +25,17 @@
2525
#include <wolfssl/wolfcrypt/random.h>
2626
#include <wolfssl/wolfcrypt/error-crypt.h>
2727

28-
/*
29-
./configure --enable-curve25519 --enable-ed25519 && make && sudo make install
30-
gcc -o curve25519_test -lwolfssl curve25519_test.c
31-
32-
./configure --enable-curve25519 --enable-ed25519 --enable-debug --disable-shared && make
33-
gcc -g -o curve25519_test -I. ./src/.libs/libwolfssl.a curve25519_test.c
28+
/* Build option A: wolfssl as dynamic shared library.
29+
*
30+
* ./configure --enable-curve25519 && make && sudo make install
31+
* gcc -o curve25519_test curve25519_test.c -lwolfssl
32+
*
33+
* Build option B: wolfssl as static library with debugging enabled.
34+
* This assumes that wolfssl and wolfssl-examples are in the same directory.
35+
*
36+
* ./configure --enable-curve25519 --enable-debug --disable-shared && make
37+
* gcc -g -o curve25519_test curve25519_test.c -I../../../wolfssl/wolfssl \
38+
* ../../../wolfssl/src/.libs/libwolfssl.a
3439
*/
3540

3641
#ifdef HAVE_CURVE25519
@@ -71,11 +76,26 @@ int curve25519_secret(const byte* priv, const byte* pub, byte* secret,
7176
{
7277
int ret;
7378
curve25519_key privKey, pubKey;
79+
#ifdef WOLFSSL_CURVE25519_BLINDING
80+
WC_RNG rng;
81+
#endif
7482

7583
ret = wc_curve25519_init(&privKey);
7684
if (ret == 0)
7785
ret = wc_curve25519_init(&pubKey);
7886

87+
#ifdef WOLFSSL_CURVE25519_BLINDING
88+
/* Normally, you would not expect Diffie-Hellman style key exchanges to
89+
* require an RNG, but if you have blinding enabled, then you need the RNG
90+
* to do blinding. */
91+
if (ret == 0) {
92+
ret = wc_InitRng(&rng);
93+
}
94+
if (ret == 0) {
95+
ret = wc_curve25519_set_rng(&privKey, &rng);
96+
}
97+
#endif
98+
7999
if (ret == 0) {
80100
ret = wc_curve25519_import_private_ex(priv, 32, &privKey, endianess);
81101
if (ret != 0) {
@@ -96,6 +116,9 @@ int curve25519_secret(const byte* priv, const byte* pub, byte* secret,
96116
secretsz, endianess);
97117
}
98118

119+
#ifdef WOLFSSL_CURVE25519_BLINDING
120+
wc_FreeRng(&rng);
121+
#endif
99122
wc_curve25519_free(&pubKey);
100123
wc_curve25519_free(&privKey);
101124
return ret;

0 commit comments

Comments
 (0)