[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet] branch master updated: elligator: code cleanup
From: |
gnunet |
Subject: |
[gnunet] branch master updated: elligator: code cleanup |
Date: |
Mon, 11 Mar 2024 00:10:31 +0100 |
This is an automated email from the git hooks/post-receive script.
pedram pushed a commit to branch master
in repository gnunet.
The following commit(s) were added to refs/heads/master by this push:
new b4a039d3c elligator: code cleanup
b4a039d3c is described below
commit b4a039d3c95a18e38fb2a7f2ec62571c311fd309
Author: Pedram Fardzadeh <p.fardzadeh@protonmail.com>
AuthorDate: Mon Mar 11 00:09:47 2024 +0100
elligator: code cleanup
---
src/include/gnunet_crypto_lib.h | 138 +++++++++++-------------
src/lib/util/crypto_elligator.c | 81 ++++++--------
src/lib/util/test_crypto_elligator.c | 17 ++-
src/service/transport/gnunet-communicator-udp.c | 5 +-
4 files changed, 107 insertions(+), 134 deletions(-)
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 168f934e1..53150bedc 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -973,7 +973,7 @@ GNUNET_CRYPTO_hash_from_string2 (const char *enc,
* @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong
encoding
*/
#define GNUNET_CRYPTO_hash_from_string(enc, result) \
- GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)
+ GNUNET_CRYPTO_hash_from_string2 (enc, strlen (enc), result)
/**
@@ -2215,15 +2215,15 @@ GNUNET_CRYPTO_eddsa_sign_ (
* @param[out] sig where to write the signature
*/
#define GNUNET_CRYPTO_eddsa_sign(priv,ps,sig) do { \
- /* check size is set correctly */ \
- GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*ps)); \
- /* check 'ps' begins with the purpose */ \
- GNUNET_static_assert (((void*) (ps)) == \
- ((void*) &(ps)->purpose)); \
- GNUNET_assert (GNUNET_OK == \
- GNUNET_CRYPTO_eddsa_sign_ (priv, \
- &(ps)->purpose, \
- sig)); \
+ /* check size is set correctly */ \
+ GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*ps)); \
+ /* check 'ps' begins with the purpose */ \
+ GNUNET_static_assert (((void*) (ps)) == \
+ ((void*) &(ps)->purpose)); \
+ GNUNET_assert (GNUNET_OK == \
+ GNUNET_CRYPTO_eddsa_sign_ (priv, \
+ &(ps)->purpose, \
+ sig)); \
} while (0)
@@ -2277,15 +2277,15 @@ GNUNET_CRYPTO_eddsa_sign_raw (
* @param[out] sig where to write the signature
*/
#define GNUNET_CRYPTO_ecdsa_sign(priv,ps,sig) do { \
- /* check size is set correctly */ \
- GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
- /* check 'ps' begins with the purpose */ \
- GNUNET_static_assert (((void*) (ps)) == \
- ((void*) &(ps)->purpose)); \
- GNUNET_assert (GNUNET_OK == \
- GNUNET_CRYPTO_ecdsa_sign_ (priv, \
- &(ps)->purpose, \
- sig)); \
+ /* check size is set correctly */ \
+ GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
+ /* check 'ps' begins with the purpose */ \
+ GNUNET_static_assert (((void*) (ps)) == \
+ ((void*) &(ps)->purpose)); \
+ GNUNET_assert (GNUNET_OK == \
+ GNUNET_CRYPTO_ecdsa_sign_ (priv, \
+ &(ps)->purpose, \
+ sig)); \
} while (0)
/**
@@ -2324,15 +2324,15 @@ GNUNET_CRYPTO_edx25519_sign_ (
* @param[out] sig where to write the signature
*/
#define GNUNET_CRYPTO_edx25519_sign(priv,ps,sig) do { \
- /* check size is set correctly */ \
- GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
- /* check 'ps' begins with the purpose */ \
- GNUNET_static_assert (((void*) (ps)) == \
- ((void*) &(ps)->purpose)); \
- GNUNET_assert (GNUNET_OK == \
- GNUNET_CRYPTO_edx25519_sign_ (priv, \
- &(ps)->purpose, \
- sig)); \
+ /* check size is set correctly */ \
+ GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
+ /* check 'ps' begins with the purpose */ \
+ GNUNET_static_assert (((void*) (ps)) == \
+ ((void*) &(ps)->purpose)); \
+ GNUNET_assert (GNUNET_OK == \
+ GNUNET_CRYPTO_edx25519_sign_ (priv, \
+ &(ps)->purpose, \
+ sig)); \
} while (0)
@@ -2676,25 +2676,24 @@ GNUNET_CRYPTO_edx25519_public_key_derive (
* @param high_y destination set to "True" if corresponding y-coordinate is >
2 ^ 254 - 10
*/
bool
-GNUNET_CRYPTO_ecdhe_elligator_direct_map (uint8_t *point, bool *high_y,
+GNUNET_CRYPTO_ecdhe_elligator_direct_map (uint8_t *point,
+ bool *high_y,
uint8_t *representative);
/**
* @ingroup crypto
- * Clears the most significant bit and second most significant bit to the
serialized representaive before applying elligator direct map.
+ * Clears the most significant bit and second most significant bit of the
serialized representaive before applying elligator direct map.
*
- * @param serialized_representative serialized version of an element of
Curves25519's finite field
+ * @param representative serialized elligator representative of an element of
Curves25519's finite field
* @param point destination for the calculated point on the curve
* @param high_y value pointed to will be set to true if corresponding
y-coordinate is > 2 ^ 254 - 10, otherwise 0. Can be set to NULL if not needed.
*/
bool
-GNUNET_CRYPTO_ecdhe_elligator_decoding (struct
- GNUNET_CRYPTO_EcdhePublicKey *point,
- bool *high_y,
- const struct
- GNUNET_CRYPTO_ElligatorRepresentative *
- seriliazed_representative);
+GNUNET_CRYPTO_ecdhe_elligator_decoding (
+ struct GNUNET_CRYPTO_EcdhePublicKey *point,
+ bool *high_y,
+ const struct GNUNET_CRYPTO_ElligatorRepresentative *representative);
/**
* @ingroup crypto
@@ -2706,33 +2705,24 @@ GNUNET_CRYPTO_ecdhe_elligator_decoding (struct
* @param representative destination for the calculated element of the finite
field
*/
bool
-GNUNET_CRYPTO_ecdhe_elligator_inverse_map (uint8_t *representative, const
- uint8_t *point,
+GNUNET_CRYPTO_ecdhe_elligator_inverse_map (uint8_t *representative,
+ const uint8_t *point,
bool high_y);
-/**
-* Initializes the elligator library
-* THis function is thread safe
-*/
-void
-GNUNET_CRYPTO_ecdhe_elligator_initialize (void);
-
/**
* @ingroup crypto
- * Generates a valid public key for elligator's inverse map by adding a lower
order point to a prime order point.
+ * Generates a valid public key for elligator's inverse map by adding a lower
order point to a prime order point
+ * following Method 1 in description https://elligator.org/key-exchange
section Step 2: Generate a “special” public key
*
* @param pub valid public key for elligator inverse map
* @param pk private key for generating valid public key
+ * @return GNUNET_OK on success
*/
-int
-GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (unsigned char
- pub[
-
crypto_scalarmult_SCALARBYTES
- ],
- struct
-
GNUNET_CRYPTO_EcdhePrivateKey
- *pk);
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (
+ struct GNUNET_CRYPTO_EcdhePublicKey *pub,
+ struct GNUNET_CRYPTO_EcdhePrivateKey *pk);
/**
@@ -2758,12 +2748,10 @@ GNUNET_CRYPTO_ecdhe_elligator_key_create (
* @return #GNUNET_SYSERR on error, #GNUNET_OK on success
*/
enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_elligator_kem_encaps (const struct
- GNUNET_CRYPTO_EddsaPublicKey *pub,
- struct
- GNUNET_CRYPTO_ElligatorRepresentative
- *r,
- struct GNUNET_HashCode
*key_material);
+GNUNET_CRYPTO_eddsa_elligator_kem_encaps (
+ const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
+ struct GNUNET_CRYPTO_ElligatorRepresentative *r,
+ struct GNUNET_HashCode *key_material);
/**
* @ingroup crypto
@@ -2776,12 +2764,10 @@ GNUNET_CRYPTO_eddsa_elligator_kem_encaps (const struct
* @return #GNUNET_SYSERR on error, #GNUNET_OK on success
*/
enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_elligator_kem_decaps (const struct
- GNUNET_CRYPTO_EddsaPrivateKey *priv,
- const struct
- GNUNET_CRYPTO_ElligatorRepresentative
- *r,
- struct GNUNET_HashCode
*key_material);
+GNUNET_CRYPTO_eddsa_elligator_kem_decaps (
+ const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
+ const struct GNUNET_CRYPTO_ElligatorRepresentative *r,
+ struct GNUNET_HashCode *key_material);
/**
@@ -4267,15 +4253,15 @@ GNUNET_CRYPTO_sign_raw_ (
* @param[out] sig where to write the signature
*/
#define GNUNET_CRYPTO_sign(priv,ps,sig) do { \
- /* check size is set correctly */
\
- GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps)));
\
- /* check 'ps' begins with the purpose */
\
- GNUNET_static_assert (((void*) (ps)) ==
\
- ((void*) &(ps)->purpose));
\
- GNUNET_assert (GNUNET_OK ==
\
- GNUNET_CRYPTO_sign_ (priv, \
- &(ps)->purpose, \
- sig)); \
+ /* check size is set correctly */ \
+ GNUNET_assert (ntohl ((ps)->purpose.size) == sizeof (*(ps))); \
+ /* check 'ps' begins with the purpose */ \
+ GNUNET_static_assert (((void*) (ps)) == \
+ ((void*) &(ps)->purpose)); \
+ GNUNET_assert (GNUNET_OK == \
+ GNUNET_CRYPTO_sign_ (priv, \
+ &(ps)->purpose, \
+ sig)); \
} while (0)
diff --git a/src/lib/util/crypto_elligator.c b/src/lib/util/crypto_elligator.c
index b46d30d6f..11bc086a9 100644
--- a/src/lib/util/crypto_elligator.c
+++ b/src/lib/util/crypto_elligator.c
@@ -218,8 +218,8 @@ encode_bytes (uint8_t *bytes, mp_limb_t *number)
}
-void
-GNUNET_CRYPTO_ecdhe_elligator_initialize (void)
+void __attribute__ ((constructor))
+GNUNET_CRYPTO_ecdhe_elligator_initialize ()
{
static bool initialized = false;
@@ -294,7 +294,8 @@ GNUNET_CRYPTO_ecdhe_elligator_initialize (void)
// Returns trash if the number is a quadratic non-residue
static void
-least_square_root (mp_limb_t *root, const mp_limb_t *number,
+least_square_root (mp_limb_t *root,
+ const mp_limb_t *number,
mp_limb_t *scratch_space)
{
mp_limb_t a[P_LIMBS + P_LIMBS];
@@ -331,8 +332,8 @@ least_square_root (mp_limb_t *root, const mp_limb_t *number,
bool
-GNUNET_CRYPTO_ecdhe_elligator_inverse_map (uint8_t *representative, const
- uint8_t *point,
+GNUNET_CRYPTO_ecdhe_elligator_inverse_map (uint8_t *representative,
+ const uint8_t *point,
bool high_y)
{
mp_limb_t scratch_space[scratch_space_length];
@@ -384,7 +385,8 @@ GNUNET_CRYPTO_ecdhe_elligator_inverse_map (uint8_t
*representative, const
bool
-GNUNET_CRYPTO_ecdhe_elligator_direct_map (uint8_t *point, bool *high_y,
+GNUNET_CRYPTO_ecdhe_elligator_direct_map (uint8_t *point,
+ bool *high_y,
uint8_t *representative)
{
mp_limb_t scratch_space[scratch_space_length];
@@ -442,14 +444,11 @@ GNUNET_CRYPTO_ecdhe_elligator_direct_map (uint8_t *point,
bool *high_y,
}
-// Removes most significant bit and second most significant bit before
applying elligator direct map
bool
-GNUNET_CRYPTO_ecdhe_elligator_decoding (struct
- GNUNET_CRYPTO_EcdhePublicKey *point,
- bool *high_y,
- const struct
- GNUNET_CRYPTO_ElligatorRepresentative *
- representative)
+GNUNET_CRYPTO_ecdhe_elligator_decoding (
+ struct GNUNET_CRYPTO_EcdhePublicKey *point,
+ bool *high_y,
+ const struct GNUNET_CRYPTO_ElligatorRepresentative *representative)
{
// if sign of direct map transformation not needed throw it away
bool high_y_local;
@@ -470,8 +469,8 @@ GNUNET_CRYPTO_ecdhe_elligator_decoding (struct
static bool
-Elligator_2_Curve25519_convert_from_Ed25519 (uint8_t *point, const
- uint8_t *source)
+convert_from_ed_to_curve (uint8_t *point,
+ const uint8_t *source)
{
mp_limb_t scratch_space[scratch_space_length];
@@ -535,16 +534,10 @@ Elligator_2_Curve25519_convert_from_Ed25519 (uint8_t
*point, const
}
-// Would call GNUNET_CRYPTO_ecdhe_key_create (struct
GNUNET_CRYPTO_EcdhePrivateKey *pk) for pk which is not clamped
-// Following Method 1 in description https://elligator.org/key-exchange
section Step 2: Generate a “special” public key
-int
-GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (unsigned char
- pub[
-
crypto_scalarmult_SCALARBYTES
- ],
- struct
-
GNUNET_CRYPTO_EcdhePrivateKey
- *pk)
+enum GNUNET_GenericReturnValue
+GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (
+ struct GNUNET_CRYPTO_EcdhePublicKey *pub,
+ struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
{
// eHigh
// crypto_scalarmult_ed25519_base clamps the scalar pk->d and return only 0
if pk->d is zero
@@ -563,7 +556,7 @@ GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (unsigned
char
return GNUNET_SYSERR;
}
- if (Elligator_2_Curve25519_convert_from_Ed25519 (pub, edPub) == false)
+ if (convert_from_ed_to_curve (pub->q_y, edPub) == false)
{
return GNUNET_SYSERR;
}
@@ -578,7 +571,7 @@ GNUNET_CRYPTO_ecdhe_elligator_key_create (
{
// inverse map can fail for some public keys generated by
GNUNET_CRYPTO_ecdhe_elligator_generate_public_key
bool validKey = 0;
- unsigned char pub[crypto_scalarmult_SCALARBYTES];
+ struct GNUNET_CRYPTO_EcdhePublicKey pub = {0};
int8_t random_tweak;
bool high_y;
bool msb_set;
@@ -592,7 +585,7 @@ GNUNET_CRYPTO_ecdhe_elligator_key_create (
// Continue if generate_public_key fails
if (GNUNET_SYSERR ==
- GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (pub, pk))
+ GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (&pub, pk))
{
continue;
}
@@ -602,12 +595,12 @@ GNUNET_CRYPTO_ecdhe_elligator_key_create (
sizeof(int8_t));
high_y = random_tweak & 1;
- validKey = GNUNET_CRYPTO_ecdhe_elligator_inverse_map ((unsigned
- char*) &(repr->r),
- (unsigned char*) pub,
- high_y ?
- GNUNET_YES :
- GNUNET_NO);
+ validKey = GNUNET_CRYPTO_ecdhe_elligator_inverse_map (
+ (unsigned char*) &(repr->r),
+ (unsigned char*) pub.q_y,
+ high_y ?
+ GNUNET_YES :
+ GNUNET_NO);
}
// Setting most significant bit and second most significant bit randomly
@@ -625,12 +618,10 @@ GNUNET_CRYPTO_ecdhe_elligator_key_create (
enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_elligator_kem_encaps (const struct
- GNUNET_CRYPTO_EddsaPublicKey *pub,
- struct
- GNUNET_CRYPTO_ElligatorRepresentative
- *r,
- struct GNUNET_HashCode *key_material)
+GNUNET_CRYPTO_eddsa_elligator_kem_encaps (
+ const struct GNUNET_CRYPTO_EddsaPublicKey *pub,
+ struct GNUNET_CRYPTO_ElligatorRepresentative *r,
+ struct GNUNET_HashCode *key_material)
{
struct GNUNET_CRYPTO_EcdhePrivateKey sk;
@@ -641,12 +632,10 @@ GNUNET_CRYPTO_eddsa_elligator_kem_encaps (const struct
enum GNUNET_GenericReturnValue
-GNUNET_CRYPTO_eddsa_elligator_kem_decaps (const struct
- GNUNET_CRYPTO_EddsaPrivateKey *priv,
- const struct
- GNUNET_CRYPTO_ElligatorRepresentative
- *r,
- struct GNUNET_HashCode *key_material)
+GNUNET_CRYPTO_eddsa_elligator_kem_decaps (
+ const struct GNUNET_CRYPTO_EddsaPrivateKey *priv,
+ const struct GNUNET_CRYPTO_ElligatorRepresentative *r,
+ struct GNUNET_HashCode *key_material)
{
struct GNUNET_CRYPTO_EcdhePublicKey pub;
GNUNET_CRYPTO_ecdhe_elligator_decoding (&pub, NULL, r);
diff --git a/src/lib/util/test_crypto_elligator.c
b/src/lib/util/test_crypto_elligator.c
index bd357a259..8fd4d4093 100644
--- a/src/lib/util/test_crypto_elligator.c
+++ b/src/lib/util/test_crypto_elligator.c
@@ -94,11 +94,11 @@ testGeneratePkScalarMult (void)
&pk,
sizeof (struct GNUNET_CRYPTO_EcdhePrivateKey));
- unsigned char pubWholeCurve[crypto_scalarmult_SCALARBYTES];
+ struct GNUNET_CRYPTO_EcdhePublicKey pubWholeCurve = {0};
unsigned char pubPrimeCurve[crypto_scalarmult_SCALARBYTES];
- if (GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (pubWholeCurve, &pk) ==
- -1)
+ if (GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (&pubWholeCurve,
+ &pk) == -1)
{
return GNUNET_SYSERR;
}
@@ -111,9 +111,9 @@ testGeneratePkScalarMult (void)
// TODO: Currently utilizing ecdsa function for ecdhe testing, due to
clamping. Clean this part later.
struct GNUNET_CRYPTO_EcdsaPrivateKey clampedPk;
GNUNET_CRYPTO_ecdsa_key_create (&clampedPk);
- crypto_scalarmult_base (pubWholeCurve, clampedPk.d);
+ crypto_scalarmult_base (pubWholeCurve.q_y, clampedPk.d);
crypto_scalarmult_base (pubPrimeCurve, clampedPk.d);
- if (memcmp (pubWholeCurve, pubPrimeCurve, sizeof(pubWholeCurve)) != 0)
+ if (memcmp (pubWholeCurve.q_y, pubPrimeCurve, sizeof(pubWholeCurve)) != 0)
{
return GNUNET_SYSERR;
}
@@ -133,15 +133,15 @@ testInverseDirect (void)
struct GNUNET_CRYPTO_EcdhePrivateKey pk;
GNUNET_CRYPTO_ecdhe_elligator_key_create (&repr, &pk);
- unsigned char pub[crypto_scalarmult_SCALARBYTES];
- if (GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (pub, &pk) == -1)
+ struct GNUNET_CRYPTO_EcdhePublicKey pub = {0};
+ if (GNUNET_CRYPTO_ecdhe_elligator_generate_public_key (&pub, &pk) == -1)
{
return GNUNET_SYSERR;
}
GNUNET_CRYPTO_ecdhe_elligator_decoding (&point, NULL, &repr);
- if (memcmp (pub, point.q_y, sizeof(point.q_y)) != 0)
+ if (memcmp (pub.q_y, point.q_y, sizeof(point.q_y)) != 0)
{
return GNUNET_SYSERR;
}
@@ -263,7 +263,6 @@ elligatorKEM ()
int
main (int argc, char *argv[])
{
- GNUNET_CRYPTO_ecdhe_elligator_initialize ();
int failure_count = 0;
diff --git a/src/service/transport/gnunet-communicator-udp.c
b/src/service/transport/gnunet-communicator-udp.c
index 3d8e980ed..28729bb73 100644
--- a/src/service/transport/gnunet-communicator-udp.c
+++ b/src/service/transport/gnunet-communicator-udp.c
@@ -67,7 +67,7 @@
* How often do we scan for changes to our network interfaces?
*/
#define INTERFACE_SCAN_FREQUENCY \
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
/**
* How long do we believe our addresses to remain up (before
@@ -76,7 +76,7 @@
#define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_UNIT_HOURS
#define WORKING_QUEUE_INTERVALL \
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,1)
+ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,1)
/**
* AES key size.
@@ -3529,7 +3529,6 @@ run (void *cls,
int
main (int argc, char *const *argv)
{
- GNUNET_CRYPTO_ecdhe_elligator_initialize ();
static const struct GNUNET_GETOPT_CommandLineOption options[] = {
GNUNET_GETOPT_OPTION_END
};
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnunet] branch master updated: elligator: code cleanup,
gnunet <=