Skip to content

Commit 911e996

Browse files
Merge pull request #9546 from SparkiDev/curve25519_base_smul_improv
Curve25519: improved smul
2 parents 498b86f + f54266c commit 911e996

File tree

15 files changed

+15813
-3919
lines changed

15 files changed

+15813
-3919
lines changed

configure.ac

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10275,6 +10275,12 @@ then
1027510275
AM_CFLAGS="$AM_CFLAGS -DNO_CURVED25519_128BIT"
1027610276
fi
1027710277
10278+
if test "$ENABLED_CURVE25519" = "ed"
10279+
then
10280+
AM_CFLAGS="$AM_CFLAGS -DWOLFSSL_CURVE25519_USE_ED25519"
10281+
AM_CCASFLAGS="$AM_CCASFLAGS -DWOLFSSL_CURVE25519_USE_ED25519"
10282+
fi
10283+
1027810284
AM_CFLAGS="$AM_CFLAGS -DHAVE_CURVE25519"
1027910285
AM_CCASFLAGS="$AM_CCASFLAGS -DHAVE_CURVE25519"
1028010286
ENABLED_FEMATH=yes

src/include.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,7 @@ endif !BUILD_FIPS_V6_PLUS
14011401

14021402
if BUILD_FEMATH
14031403
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/fe_low_mem.c
1404+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/ge_operations.c
14041405
if BUILD_CURVE25519_INTELASM
14051406
if !BUILD_X86_ASM
14061407
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/fe_x25519_asm.S
@@ -1460,8 +1461,8 @@ endif BUILD_FEMATH
14601461

14611462
if BUILD_GEMATH
14621463
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/ge_low_mem.c
1463-
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/ge_operations.c
14641464
if !BUILD_FEMATH
1465+
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/ge_operations.c
14651466
if BUILD_CURVE25519_INTELASM
14661467
if !BUILD_X86_ASM
14671468
src_libwolfssl@LIBSUFFIX@_la_SOURCES += wolfcrypt/src/fe_x25519_asm.S

wolfcrypt/src/curve25519.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#ifdef HAVE_CURVE25519
3232

3333
#include <wolfssl/wolfcrypt/curve25519.h>
34+
#include <wolfssl/wolfcrypt/ge_operations.h>
3435
#ifdef NO_INLINE
3536
#include <wolfssl/wolfcrypt/misc.h>
3637
#else
@@ -54,6 +55,8 @@
5455
#error "Blinding not needed nor available for small implementation"
5556
#elif defined(USE_INTEL_SPEEDUP) || defined(WOLFSSL_ARMASM)
5657
#error "Blinding not needed nor available for assembly implementation"
58+
#elif defined(WOLFSSL_CURVE25519_USE_ED25519)
59+
#error "Ed25519 base scalar mult cannot be used with blinding "
5760
#endif
5861
#endif
5962

@@ -72,13 +75,16 @@ const curve25519_set_type curve25519_sets[] = {
7275
}
7376
};
7477

78+
#if !defined(WOLFSSL_CURVE25519_USE_ED25519) || \
79+
defined(WOLFSSL_CURVE25519_BLINDING)
7580
static const word32 kCurve25519BasePoint[CURVE25519_KEYSIZE/sizeof(word32)] = {
7681
#ifdef BIG_ENDIAN_ORDER
7782
0x09000000
7883
#else
7984
9
8085
#endif
8186
};
87+
#endif /* !WOLFSSL_CURVE25519_USE_ED25519 || WOLFSSL_CURVE25519_BLINDING */
8288

8389
/* Curve25519 private key must be less than order */
8490
/* These functions clamp private k and check it */
@@ -154,7 +160,31 @@ int wc_curve25519_make_pub(int public_size, byte* pub, int private_size,
154160

155161
SAVE_VECTOR_REGISTERS(return _svr_ret;);
156162

163+
#if defined(WOLFSSL_CURVE25519_USE_ED25519)
164+
{
165+
ge_p3 A;
166+
167+
ge_scalarmult_base(&A, priv);
168+
#ifndef CURVE25519_SMALL
169+
fe_add(A.X, A.Z, A.Y);
170+
fe_sub(A.T, A.Z, A.Y);
171+
fe_invert(A.T, A.T);
172+
fe_mul(A.T, A.X, A.T);
173+
fe_tobytes(pub, A.T);
174+
#else
175+
lm_add(A.X, A.Z, A.Y);
176+
lm_sub(A.T, A.Z, A.Y);
177+
lm_invert(A.T, A.T);
178+
lm_mul(pub, A.X, A.T);
179+
#endif
180+
ret = 0;
181+
}
182+
#elif defined(CURVED25519_X64) || (defined(WOLFSSL_ARMASM) && \
183+
defined(__aarch64__))
184+
ret = curve25519_base(pub, priv);
185+
#else
157186
ret = curve25519(pub, priv, (byte*)kCurve25519BasePoint);
187+
#endif
158188

159189
RESTORE_VECTOR_REGISTERS();
160190
#else

wolfcrypt/src/ed25519.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,7 @@ static int ed25519_hash_init(ed25519_key* key, wc_Sha512 *sha)
8888
{
8989
int ret;
9090

91-
#ifndef WOLFSSL_ED25519_PERSISTENT_SHA
92-
/* when not using persistent SHA, we'll zero the sha param */
93-
XMEMSET(sha, 0, sizeof(wc_Sha512));
94-
#endif
95-
9691
ret = wc_InitSha512_ex(sha, key->heap,
97-
9892
#if defined(WOLF_CRYPTO_CB)
9993
key->devId
10094
#else
@@ -103,8 +97,9 @@ static int ed25519_hash_init(ed25519_key* key, wc_Sha512 *sha)
10397
);
10498

10599
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
106-
if (ret == 0)
100+
if (ret == 0) {
107101
key->sha_clean_flag = 1;
102+
}
108103
#endif
109104

110105
return ret;
@@ -114,8 +109,10 @@ static int ed25519_hash_init(ed25519_key* key, wc_Sha512 *sha)
114109
static int ed25519_hash_reset(ed25519_key* key)
115110
{
116111
int ret;
117-
if (key->sha_clean_flag)
112+
113+
if (key->sha_clean_flag) {
118114
ret = 0;
115+
}
119116
else {
120117
wc_Sha512Free(&key->sha);
121118
ret = wc_InitSha512_ex(&key->sha, key->heap,
@@ -128,6 +125,7 @@ static int ed25519_hash_reset(ed25519_key* key)
128125
if (ret == 0)
129126
key->sha_clean_flag = 1;
130127
}
128+
131129
return ret;
132130
}
133131
#endif /* WOLFSSL_ED25519_PERSISTENT_SHA */
@@ -136,8 +134,9 @@ static int ed25519_hash_update(ed25519_key* key, wc_Sha512 *sha,
136134
const byte* data, word32 len)
137135
{
138136
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
139-
if (key->sha_clean_flag)
137+
if (key->sha_clean_flag) {
140138
key->sha_clean_flag = 0;
139+
}
141140
#else
142141
(void)key;
143142
#endif
@@ -148,8 +147,9 @@ static int ed25519_hash_final(ed25519_key* key, wc_Sha512 *sha, byte* hash)
148147
{
149148
int ret = wc_Sha512Final(sha, hash);
150149
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
151-
if (ret == 0)
150+
if (ret == 0) {
152151
key->sha_clean_flag = 1;
152+
}
153153
#else
154154
(void)key;
155155
#endif
@@ -187,16 +187,15 @@ static int ed25519_hash(ed25519_key* key, const byte* in, word32 inLen,
187187
#else
188188
ret = ed25519_hash_init(key, sha);
189189
#endif
190-
if (ret < 0)
191-
return ret;
192-
193-
ret = ed25519_hash_update(key, sha, in, inLen);
194-
if (ret == 0)
195-
ret = ed25519_hash_final(key, sha, hash);
190+
if (ret == 0) {
191+
ret = ed25519_hash_update(key, sha, in, inLen);
192+
if (ret == 0)
193+
ret = ed25519_hash_final(key, sha, hash);
196194

197-
#ifndef WOLFSSL_ED25519_PERSISTENT_SHA
198-
ed25519_hash_free(key, sha);
199-
#endif
195+
#ifndef WOLFSSL_ED25519_PERSISTENT_SHA
196+
ed25519_hash_free(key, sha);
197+
#endif
198+
}
200199

201200
return ret;
202201
}

0 commit comments

Comments
 (0)