33#define ED25519_NO_SEED 1
44#include < ed_25519.h>
55
6- // For weaker ESP32 boards, we use libsodium for cryptographic operations to reduce stack usage
7- #ifdef USE_ESP32_ENCRYPTION
8- #include < sodium.h>
9- #endif
10-
116namespace mesh {
127
138Identity::Identity () {
@@ -19,11 +14,7 @@ Identity::Identity(const char* pub_hex) {
1914}
2015
2116bool Identity::verify (const uint8_t * sig, const uint8_t * message, int msg_len) const {
22- #ifdef USE_ESP32_ENCRYPTION
23- return crypto_sign_ed25519_verify_detached (sig, message, msg_len, pub_key) == 0 ;
24- #else
25- return ed25519_verify (sig, message, msg_len, pub_key);
26- #endif
17+ return ed25519_verify (sig, message, msg_len, pub_key);
2718}
2819
2920bool Identity::readFrom (Stream& s) {
@@ -41,29 +32,14 @@ void Identity::printTo(Stream& s) const {
4132LocalIdentity::LocalIdentity () {
4233 memset (prv_key, 0 , sizeof (prv_key));
4334}
44-
4535LocalIdentity::LocalIdentity (const char * prv_hex, const char * pub_hex) : Identity(pub_hex) {
4636 Utils::fromHex (prv_key, PRV_KEY_SIZE, prv_hex);
4737}
4838
4939LocalIdentity::LocalIdentity (RNG* rng) {
5040 uint8_t seed[SEED_SIZE];
5141 rng->random (seed, SEED_SIZE);
52-
53- #ifdef USE_ESP32_ENCRYPTION
54- // Use libsodium for keypair generation on ESP32 to reduce stack usage
55- // NOTE: Format differences between implementations:
56- // - The current ed25519 implementation (orlp/ed25519) uses a 64-byte private key format
57- // - Libsodium also uses a 64-byte format for Ed25519 secret keys, where:
58- // * First 32 bytes: the actual private key seed
59- // * Last 32 bytes: the corresponding public key
60-
61- // Generate keypair using libsodium with the provided seed
62- // This avoids the deep stack usage of the default implementation
63- crypto_sign_ed25519_seed_keypair (pub_key, prv_key, seed);
64- #else
65- ed25519_create_keypair (pub_key, prv_key, seed);
66- #endif
42+ ed25519_create_keypair (pub_key, prv_key, seed);
6743}
6844
6945bool LocalIdentity::readFrom (Stream& s) {
@@ -101,46 +77,17 @@ void LocalIdentity::readFrom(const uint8_t* src, size_t len) {
10177 memcpy (pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE);
10278 } else if (len == PRV_KEY_SIZE) {
10379 memcpy (prv_key, src, PRV_KEY_SIZE);
104-
105- #ifdef USE_ESP32_ENCRYPTION
106- // In libsodium, the private key already contains the public key in its last 32 bytes
107- // We can just extract it directly, avoiding the expensive derivation calculation
108- memcpy (pub_key, prv_key + 32 , 32 );
109- #else
110- // now need to re-calculate the pub_key
111- ed25519_derive_pub (pub_key, prv_key);
112- #endif
80+ // now need to re-calculate the pub_key
81+ ed25519_derive_pub (pub_key, prv_key);
11382 }
11483}
11584
11685void LocalIdentity::sign (uint8_t * sig, const uint8_t * message, int msg_len) const {
117- #ifdef USE_ESP32_ENCRYPTION
118- crypto_sign_ed25519_detached (sig, NULL , message, msg_len, prv_key);
119- #else
120- ed25519_sign (sig, message, msg_len, pub_key, prv_key);
121- #endif
86+ ed25519_sign (sig, message, msg_len, pub_key, prv_key);
12287}
12388
12489void LocalIdentity::calcSharedSecret (uint8_t * secret, const uint8_t * other_pub_key) {
125- #ifdef USE_ESP32_ENCRYPTION
126- // NOTE: To calculate a shared secret with Ed25519 keys and libsodium, we need to:
127- // Convert the Ed25519 keys to Curve25519 (X25519) format
128- // Perform the key exchange using the converted keys
129- //
130- // The default implementation handles this conversion internally,
131- // but with libsodium we need to do these steps explicitly.
132- unsigned char x25519_pk[crypto_scalarmult_curve25519_BYTES];
133- unsigned char x25519_sk[crypto_scalarmult_curve25519_BYTES];
134-
135- // Convert Ed25519 keys to Curve25519 keys for ECDH
136- crypto_sign_ed25519_pk_to_curve25519 (x25519_pk, other_pub_key);
137- crypto_sign_ed25519_sk_to_curve25519 (x25519_sk, prv_key);
138-
139- // Calculate shared secret using X25519
140- crypto_scalarmult_curve25519 (secret, x25519_sk, x25519_pk);
141- #else
142- ed25519_key_exchange (secret, other_pub_key, prv_key);
143- #endif
90+ ed25519_key_exchange (secret, other_pub_key, prv_key);
14491}
14592
14693}
0 commit comments