Update to Crypto lib in Meshtatic org

This commit is contained in:
Jonathan Bennett
2026-05-13 13:25:10 -05:00
parent 08c2a610ee
commit 171a8a9c7e
12 changed files with 16 additions and 1420 deletions
+6 -52
View File
@@ -6,9 +6,9 @@
#if !(MESHTASTIC_EXCLUDE_PKI)
#include "HardwareRNG.h"
#include "NodeDB.h"
#include "XEdDSA.h"
#include "aes-ccm.h"
#include "meshUtils.h"
#include "xeddsa.h"
#include <Crypto.h>
#include <Curve25519.h>
#include <Ed25519.h>
@@ -21,9 +21,6 @@
#endif
#if !(MESHTASTIC_EXCLUDE_PKI_KEYGEN)
#if !defined(ARCH_STM32WL)
#define CryptRNG RNG
#endif
/**
* Create a public/private key pair with Curve25519.
@@ -54,7 +51,7 @@ void CryptoEngine::generateKeyPair(uint8_t *pubKey, uint8_t *privKey)
Curve25519::dh1(public_key, private_key);
memcpy(pubKey, public_key, sizeof(public_key));
memcpy(privKey, private_key, sizeof(private_key));
priv_curve_to_ed_keys(private_key, xeddsa_private_key, xeddsa_public_key);
XEdDSA::priv_curve_to_ed_keys(private_key, xeddsa_private_key, xeddsa_public_key);
}
/**
@@ -74,7 +71,7 @@ bool CryptoEngine::regeneratePublicKey(uint8_t *pubKey, uint8_t *privKey)
}
memcpy(private_key, privKey, sizeof(private_key));
memcpy(public_key, pubKey, sizeof(public_key));
priv_curve_to_ed_keys(private_key, xeddsa_private_key, xeddsa_public_key);
XEdDSA::priv_curve_to_ed_keys(private_key, xeddsa_private_key, xeddsa_public_key);
} else {
LOG_WARN("X25519 key generation failed due to blank private key");
return false;
@@ -84,8 +81,8 @@ bool CryptoEngine::regeneratePublicKey(uint8_t *pubKey, uint8_t *privKey)
bool CryptoEngine::xeddsa_sign(uint8_t *message, size_t len, uint8_t *signature)
{
Ed25519::sign(signature, xeddsa_private_key, xeddsa_public_key, message,
len); // sign will need modified to use the raw secret scalar, and not hash it first.
XEdDSA::sign(signature, xeddsa_private_key, xeddsa_public_key, message,
len); // sign will need modified to use the raw secret scalar, and not hash it first.
return true;
}
bool CryptoEngine::xeddsa_verify(uint8_t *pubKey, uint8_t *message, size_t len, uint8_t *signature)
@@ -93,7 +90,7 @@ bool CryptoEngine::xeddsa_verify(uint8_t *pubKey, uint8_t *message, size_t len,
uint8_t publicKey[32] = {0};
curve_to_ed_pub(pubKey, publicKey);
return Ed25519::verify(signature, publicKey, message, len);
return XEdDSA::verify(signature, publicKey, message, len);
}
void CryptoEngine::curve_to_ed_pub(uint8_t *curve_pubkey, uint8_t *ed_pubkey)
@@ -133,49 +130,6 @@ void CryptoEngine::curve_to_ed_pub(uint8_t *curve_pubkey, uint8_t *ed_pubkey)
// need to convert the pubkey y = ( u - 1) * inv( u + 1) (mod p).
}
void CryptoEngine::priv_curve_to_ed_keys(uint8_t *curve_privkey, uint8_t *ed_privkey, uint8_t *ed_pubkey)
{
limb_t a[NUM_LIMBS_256BIT];
limb_t a2[NUM_LIMBS_256BIT];
uint8_t negKey[32] = {0};
Ed25519::Point ptA;
Ed25519::Point ptA2;
for (uint8_t i = 0; i < 32; i++) {
ed_privkey[i] = curve_privkey[i];
}
ed_privkey[0] &= 0xF8;
ed_privkey[31] &= 0x7F;
ed_privkey[31] |= 0x40;
Ed25519::deriveKeys(nullptr, a, ed_privkey);
Ed25519::mul(ptA, a);
Ed25519::encodePoint(ed_pubkey, ptA);
clean(a);
clean(ptA);
// check sign
if ((ed_pubkey[31] & 0x80) >> 7 == 0) {
return;
}
sc_muladd(negKey, MINUS_ONE, ed_privkey, ZERO);
for (uint8_t i = 0; i < 32; i++) {
ed_privkey[i] = negKey[i];
}
BigNumberUtil::unpackLE(a2, NUM_LIMBS_256BIT, negKey, 32);
Ed25519::mul(ptA2, a2);
Ed25519::encodePoint(ed_pubkey, ptA2);
if ((ed_pubkey[31] & 0x80) >> 7 == 0) {
}
// Clean up and exit.
clean(a);
clean(ptA);
}
bool CryptoEngine::ensurePkiKeys(meshtastic_Config_SecurityConfig &security, meshtastic_User &user){
if (user.is_licensed) {
-1
View File
@@ -90,7 +90,6 @@ class CryptoEngine
uint8_t xeddsa_public_key[32] = {0};
uint8_t xeddsa_private_key[32] = {0};
void curve_to_ed_pub(uint8_t *curve_pubkey, uint8_t *ed_pubkey);
void priv_curve_to_ed_keys(uint8_t *curve_privkey, uint8_t *ed_privkey, uint8_t *ed_pubkey);
#endif
/**
* Init our 128 bit nonce for a new packet
-1318
View File
File diff suppressed because it is too large Load Diff
-40
View File
@@ -1,40 +0,0 @@
#include <Arduino.h>
// imported from SUPERCOP by Daniel J. Bernstein which is public domain
// Byte-representation of the scalar value of 0 on the Ed25519 curve. Needed by `sc_neg`.
static const uint8_t ZERO[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// Byte-representation of the scalar value of -1 on the Ed25519 curve. Needed by `sc_neg`.
static const uint8_t MINUS_ONE[32] = {0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7,
0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
typedef uint64_t crypto_uint64;
typedef int64_t crypto_int64;
typedef int32_t crypto_int32;
typedef crypto_int32 fe[10];
static crypto_uint64 load_3(const unsigned char *in);
static crypto_uint64 load_4(const unsigned char *in);
void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c);
void fe_sub(fe h, fe f, fe g);
void fe_frombytes(fe h, const unsigned char *s);
void fe_1(fe h);
void fe_add(fe h, const fe f, const fe g);
void fe_sq(fe h, const fe f);
void fe_mul(fe h, const fe f, const fe g);
void fe_invert(fe out, const fe z);
void fe_tobytes(unsigned char *s, const fe h);