Skip to content

Commit

Permalink
curve25519 and ed25519 low memory
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Jun 5, 2015
1 parent 96a2980 commit e461bc7
Show file tree
Hide file tree
Showing 11 changed files with 2,060 additions and 790 deletions.
22 changes: 20 additions & 2 deletions configure.ac
Expand Up @@ -680,17 +680,28 @@ then
fi


# for using memory optimization setting on both curve25519 and ed25519
ENABLED_CURVED25519_SMALL=no

# CURVE25519
AC_ARG_ENABLE([curve25519],
[AS_HELP_STRING([--enable-curve25519],[Enable Curve25519 (default: disabled)])],
[ ENABLED_CURVE25519=$enableval ],
[ ENABLED_CURVE25519=no ]
)


if test "$ENABLED_CURVE25519" = "small"
then
AM_CFLAGS="$AM_CFLAGS -DCURVED25519_SMALL"
ENABLED_CURVED25519_SMALL=yes
ENABLED_CURVE25519=yes
fi

if test "$ENABLED_CURVE25519" = "yes"
then
ENABLED_FEMATH=yes
AM_CFLAGS="$AM_CFLAGS -DHAVE_CURVE25519"
ENABLED_FEMATH=yes
fi


Expand All @@ -705,6 +716,13 @@ AC_ARG_ENABLE([ed25519],
)


if test "$ENABLED_ED25519" = "small"
then
AM_CFLAGS="$AM_CFLAGS -DCURVED25519_SMALL"
ENABLED_CURVED25519_SMALL=yes
ENABLED_ED25519=yes
fi

if test "$ENABLED_ED25519" = "yes"
then
if test "$ENABLED_SHA512" = "no"
Expand All @@ -716,8 +734,8 @@ then
AM_CFLAGS="$AM_CFLAGS -DHAVE_ED25519"
fi


AM_CONDITIONAL([BUILD_ED25519], [test "x$ENABLED_ED25519" = "xyes"])
AM_CONDITIONAL([BUILD_CURVED25519_SMALL], [test "x$ENABLED_CURVED25519_SMALL" = "xyes"])
AM_CONDITIONAL([BUILD_FEMATH], [test "x$ENABLED_FEMATH" = "xyes"])
AM_CONDITIONAL([BUILD_GEMATH], [test "x$ENABLED_GEMATH" = "xyes"])

Expand Down
8 changes: 8 additions & 0 deletions src/include.am
Expand Up @@ -176,12 +176,20 @@ src_libwolfssl_la_SOURCES += wolfcrypt/src/ed25519.c
endif

if BUILD_FEMATH
if BUILD_CURVED25519_SMALL
src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_low_mem.c
else
src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_operations.c
endif
endif

if BUILD_GEMATH
if BUILD_CURVED25519_SMALL
src_libwolfssl_la_SOURCES += wolfcrypt/src/ge_low_mem.c
else
src_libwolfssl_la_SOURCES += wolfcrypt/src/ge_operations.c
endif
endif

if BUILD_LIBZ
src_libwolfssl_la_SOURCES += wolfcrypt/src/compress.c
Expand Down
68 changes: 1 addition & 67 deletions wolfcrypt/src/curve25519.c
Expand Up @@ -46,72 +46,6 @@ const curve25519_set_type curve25519_sets[] = {
};


/* internal function */
static int curve25519(unsigned char* q, unsigned char* n, unsigned char* p)
{
unsigned char e[32];
unsigned int i;
fe x1;
fe x2;
fe z2;
fe x3;
fe z3;
fe tmp0;
fe tmp1;
int pos;
unsigned int swap;
unsigned int b;

for (i = 0;i < 32;++i) e[i] = n[i];
e[0] &= 248;
e[31] &= 127;
e[31] |= 64;

fe_frombytes(x1,p);
fe_1(x2);
fe_0(z2);
fe_copy(x3,x1);
fe_1(z3);

swap = 0;
for (pos = 254;pos >= 0;--pos) {
b = e[pos / 8] >> (pos & 7);
b &= 1;
swap ^= b;
fe_cswap(x2,x3,swap);
fe_cswap(z2,z3,swap);
swap = b;

/* montgomery */
fe_sub(tmp0,x3,z3);
fe_sub(tmp1,x2,z2);
fe_add(x2,x2,z2);
fe_add(z2,x3,z3);
fe_mul(z3,tmp0,x2);
fe_mul(z2,z2,tmp1);
fe_sq(tmp0,tmp1);
fe_sq(tmp1,x2);
fe_add(x3,z3,z2);
fe_sub(z2,z3,z2);
fe_mul(x2,tmp1,tmp0);
fe_sub(tmp1,tmp1,tmp0);
fe_sq(z2,z2);
fe_mul121666(z3,tmp1);
fe_sq(x3,x3);
fe_add(tmp0,tmp0,z3);
fe_mul(z3,x1,z2);
fe_mul(z2,tmp1,tmp0);
}
fe_cswap(x2,x3,swap);
fe_cswap(z2,z3,swap);

fe_invert(z2,z2);
fe_mul(x2,x2,z2);
fe_tobytes(q,x2);

return 0;
}


int wc_curve25519_make_key(RNG* rng, int keysize, curve25519_key* key)
{
Expand All @@ -138,7 +72,7 @@ int wc_curve25519_make_key(RNG* rng, int keysize, curve25519_key* key)
key->k.point[31] &= 127;
key->k.point[31] |= 64;

/*compute public key*/
/* compute public key */
ret = curve25519(p, key->k.point, basepoint);

/* store keys in big endian format */
Expand Down

0 comments on commit e461bc7

Please sign in to comment.