Skip to content

Commit 86f11d0

Browse files
author
Scott Powell
committed
* LocalIdentity:: writeTo( array ) and readFrom (array )
1 parent ec92bfa commit 86f11d0

6 files changed

Lines changed: 37 additions & 0 deletions

File tree

examples/simple_repeater/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ void setup() {
330330
halt();
331331
}
332332

333+
radio.setCRC(0);
334+
333335
#ifdef SX126X_CURRENT_LIMIT
334336
radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
335337
#endif

examples/simple_secure_chat/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ void setup() {
277277
halt();
278278
}
279279

280+
radio.setCRC(0);
281+
280282
#ifdef SX126X_CURRENT_LIMIT
281283
radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
282284
#endif

lib/ed25519/ed_25519.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ int ED25519_DECLSPEC ed25519_create_seed(unsigned char *seed);
2727
#endif
2828

2929
void ED25519_DECLSPEC ed25519_create_keypair(unsigned char *public_key, unsigned char *private_key, const unsigned char *seed);
30+
void ED25519_DECLSPEC ed25519_derive_pub(unsigned char *public_key, const unsigned char *private_key);
3031
void ED25519_DECLSPEC ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key, const unsigned char *private_key);
3132
int ED25519_DECLSPEC ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key);
3233
void ED25519_DECLSPEC ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, const unsigned char *scalar);

lib/ed25519/keypair.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ void ed25519_create_keypair(unsigned char *public_key, unsigned char *private_ke
1414
ge_scalarmult_base(&A, private_key);
1515
ge_p3_tobytes(public_key, &A);
1616
}
17+
18+
void ed25519_derive_pub(unsigned char *public_key, const unsigned char *private_key) {
19+
ge_p3 A;
20+
21+
ge_scalarmult_base(&A, private_key);
22+
ge_p3_tobytes(public_key, &A);
23+
}

src/Identity.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,29 @@ void LocalIdentity::printTo(Stream& s) const {
5959
s.print("prv_key: "); Utils::printHex(s, prv_key, PRV_KEY_SIZE); s.println();
6060
}
6161

62+
size_t LocalIdentity::writeTo(uint8_t* dest, size_t max_len) {
63+
if (max_len < PRV_KEY_SIZE) return 0; // not big enough
64+
65+
if (max_len < PRV_KEY_SIZE + PUB_KEY_SIZE) { // only room for prv_key
66+
memcpy(dest, prv_key, PRV_KEY_SIZE);
67+
return PRV_KEY_SIZE;
68+
}
69+
memcpy(dest, prv_key, PRV_KEY_SIZE); // otherwise can fit prv + pub keys
70+
memcpy(&dest[PRV_KEY_SIZE], pub_key, PUB_KEY_SIZE);
71+
return PRV_KEY_SIZE + PUB_KEY_SIZE;
72+
}
73+
74+
void LocalIdentity::readFrom(const uint8_t* src, size_t len) {
75+
if (len == PRV_KEY_SIZE + PUB_KEY_SIZE) { // has prv + pub keys
76+
memcpy(prv_key, src, PRV_KEY_SIZE);
77+
memcpy(pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE);
78+
} else if (len == PRV_KEY_SIZE) {
79+
memcpy(prv_key, src, PRV_KEY_SIZE);
80+
// now need to re-calculate the pub_key
81+
ed25519_derive_pub(pub_key, prv_key);
82+
}
83+
}
84+
6285
void LocalIdentity::sign(uint8_t* sig, const uint8_t* message, int msg_len) const {
6386
ed25519_sign(sig, message, msg_len, pub_key, prv_key);
6487
}

src/Identity.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class LocalIdentity : public Identity {
7676
bool readFrom(Stream& s);
7777
bool writeTo(Stream& s) const;
7878
void printTo(Stream& s) const;
79+
size_t writeTo(uint8_t* dest, size_t max_len);
80+
void readFrom(const uint8_t* src, size_t len);
7981
};
8082

8183
}

0 commit comments

Comments
 (0)