Skip to content

Commit f059e57

Browse files
committed
- Use the new Crypto, TypeConversion and random() functionality added to the Arduino core, instead of the versions local to the mesh library.
- Rearrange class variables to minimize storage padding. - Add protected getters for EspnowMeshBackend and MeshBackendBase components. - Partially update README.md
1 parent 595fb23 commit f059e57

34 files changed

+219
-1582
lines changed

libraries/ESP8266WiFiMesh/README.md

Lines changed: 71 additions & 61 deletions
Large diffs are not rendered by default.

libraries/ESP8266WiFiMesh/examples/HelloEspnow/HelloEspnow.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ void networkFilter(int numberOfNetworks, MeshBackendBase &meshInstance) {
138138

139139
if (targetNodeID < TypeCast::stringToUint64(meshInstance.getNodeID())) {
140140
if (EspnowMeshBackend *espnowInstance = TypeCast::meshBackendCast<EspnowMeshBackend *>(&meshInstance)) {
141-
espnowInstance->connectionQueue().push_back(networkIndex);
141+
espnowInstance->connectionQueue().emplace_back(networkIndex);
142142
} else if (TcpIpMeshBackend *tcpIpInstance = TypeCast::meshBackendCast<TcpIpMeshBackend *>(&meshInstance)) {
143-
tcpIpInstance->connectionQueue().push_back(networkIndex);
143+
tcpIpInstance->connectionQueue().emplace_back(networkIndex);
144144
} else {
145145
Serial.println(F("Invalid mesh backend!"));
146146
}

libraries/ESP8266WiFiMesh/examples/HelloTcpIp/HelloTcpIp.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ void networkFilter(int numberOfNetworks, MeshBackendBase &meshInstance) {
117117

118118
if (targetNodeID < TypeCast::stringToUint64(meshInstance.getNodeID())) {
119119
if (EspnowMeshBackend *espnowInstance = TypeCast::meshBackendCast<EspnowMeshBackend *>(&meshInstance)) {
120-
espnowInstance->connectionQueue().push_back(networkIndex);
120+
espnowInstance->connectionQueue().emplace_back(networkIndex);
121121
} else if (TcpIpMeshBackend *tcpIpInstance = TypeCast::meshBackendCast<TcpIpMeshBackend *>(&meshInstance)) {
122-
tcpIpInstance->connectionQueue().push_back(networkIndex);
122+
tcpIpInstance->connectionQueue().emplace_back(networkIndex);
123123
} else {
124124
Serial.println(F("Invalid mesh backend!"));
125125
}

libraries/ESP8266WiFiMesh/src/CryptoInterface.cpp

Lines changed: 0 additions & 563 deletions
This file was deleted.

libraries/ESP8266WiFiMesh/src/CryptoInterface.h

Lines changed: 0 additions & 803 deletions
This file was deleted.

libraries/ESP8266WiFiMesh/src/EncryptedConnectionData.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ EncryptedConnectionData::EncryptedConnectionData(const uint8_t peerStaMac[6], co
5050
}
5151

5252
EncryptedConnectionData::EncryptedConnectionData(const EncryptedConnectionData &other)
53-
: _peerSessionKey(other.getPeerSessionKey()), _ownSessionKey(other.getOwnSessionKey()), _desync(other.desync()),
54-
_timeTracker(other.temporary() ? new ExpiringTimeTracker(*other.temporary()) : nullptr)
53+
: _peerSessionKey(other.getPeerSessionKey()), _ownSessionKey(other.getOwnSessionKey()),
54+
_timeTracker(other.temporary() ? new ExpiringTimeTracker(*other.temporary()) : nullptr),
55+
_desync(other.desync())
5556
{
5657
other.getPeerStaMac(_peerStaMac);
5758
other.getPeerApMac(_peerApMac);
@@ -132,16 +133,16 @@ uint64_t EncryptedConnectionData::getOwnSessionKey() const { return _ownSessionK
132133
uint64_t EncryptedConnectionData::incrementSessionKey(const uint64_t sessionKey, const uint8_t *hashKey, const uint8_t hashKeyLength)
133134
{
134135
uint8_t inputArray[8] {0};
135-
uint8_t hmacArray[CryptoInterface::SHA256_NATURAL_LENGTH] {0};
136-
CryptoInterface::sha256Hmac(TypeCast::uint64ToUint8Array(sessionKey, inputArray), 8, hashKey, hashKeyLength, hmacArray, CryptoInterface::SHA256_NATURAL_LENGTH);
136+
uint8_t hmacArray[experimental::crypto::SHA256::NATURAL_LENGTH] {0};
137+
experimental::crypto::SHA256::hmac(TypeCast::uint64ToUint8Array(sessionKey, inputArray), 8, hashKey, hashKeyLength, hmacArray, experimental::crypto::SHA256::NATURAL_LENGTH);
137138

138139
/* HMAC truncation should be OK since hmac sha256 is a PRF and we are truncating to the leftmost (MSB) bits.
139140
PRF: https://crypto.stackexchange.com/questions/26410/whats-the-gcm-sha-256-of-a-tls-protocol/26434#26434
140141
Truncate to leftmost bits: https://tools.ietf.org/html/rfc2104#section-5 */
141142
uint64_t newLeftmostBits = TypeCast::uint8ArrayToUint64(hmacArray) & EspnowProtocolInterpreter::uint64LeftmostBits;
142143

143144
if(newLeftmostBits == 0)
144-
newLeftmostBits = ((uint64_t)RANDOM_REG32 | (1 << 31)) << 32; // We never want newLeftmostBits == 0 since that would indicate an unencrypted transmission.
145+
newLeftmostBits = ((uint64_t)ESP.random() | (1 << 31)) << 32; // We never want newLeftmostBits == 0 since that would indicate an unencrypted transmission.
145146

146147
uint64_t newRightmostBits = (uint32_t)(sessionKey + 1);
147148

libraries/ESP8266WiFiMesh/src/EncryptedConnectionData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class EncryptedConnectionData {
9090
uint8_t _peerApMac[6] {0};
9191
uint64_t _peerSessionKey;
9292
uint64_t _ownSessionKey;
93+
std::unique_ptr<ExpiringTimeTracker> _timeTracker = nullptr;
9394
uint8_t _hashKey[EspnowProtocolInterpreter::hashKeyLength] {0};
9495
bool _desync = false;
95-
std::unique_ptr<ExpiringTimeTracker> _timeTracker = nullptr;
9696
};
9797

9898
#endif

libraries/ESP8266WiFiMesh/src/EspnowConnectionManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ class EspnowConnectionManager
140140

141141
ConditionalPrinter & _conditionalPrinter;
142142
EspnowDatabase & _database;
143-
144-
uint8_t _encryptedConnectionsSoftLimit = 6;
143+
144+
static ConnectionType getConnectionInfoHelper(const EncryptedConnectionLog *encryptedConnection, uint32_t *remainingDuration, uint8_t *peerMac = nullptr);
145145

146146
uint8_t _espnowEncryptedConnectionKey[EspnowProtocolInterpreter::encryptedConnectionKeyLength] {0};
147147
uint8_t _espnowHashKey[EspnowProtocolInterpreter::hashKeyLength] {0};
148-
149-
static ConnectionType getConnectionInfoHelper(const EncryptedConnectionLog *encryptedConnection, uint32_t *remainingDuration, uint8_t *peerMac = nullptr);
148+
149+
uint8_t _encryptedConnectionsSoftLimit = 6;
150150
};
151151

152152
#endif

libraries/ESP8266WiFiMesh/src/EspnowDatabase.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,6 @@ class EspnowDatabase
202202
ConditionalPrinter & _conditionalPrinter;
203203

204204
uint32_t _autoEncryptionDuration = 50;
205-
206-
uint8_t _senderMac[6] = {0};
207-
uint8_t _senderAPMac[6] = {0};
208-
209-
uint8 _espnowWiFiChannel;
210205

211206
template <typename T, typename U>
212207
static void deleteExpiredLogEntries(std::map<std::pair<U, uint64_t>, T> &logEntries, const uint32_t maxEntryLifetimeMs);
@@ -218,6 +213,11 @@ class EspnowDatabase
218213

219214
template <typename T>
220215
static void deleteExpiredLogEntries(std::list<T> &logEntries, const uint32_t maxEntryLifetimeMs);
216+
217+
uint8_t _senderMac[6] = {0};
218+
uint8_t _senderAPMac[6] = {0};
219+
220+
uint8 _espnowWiFiChannel;
221221
};
222222

223223
#endif

libraries/ESP8266WiFiMesh/src/EspnowEncryptionBroker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ namespace
3737
namespace TypeCast = MeshTypeConversionFunctions;
3838

3939
String _ongoingPeerRequestNonce;
40-
uint8_t _ongoingPeerRequestMac[6] = {0};
4140
EspnowMeshBackend *_ongoingPeerRequester = nullptr;
4241
EncryptedConnectionStatus _ongoingPeerRequestResult = EncryptedConnectionStatus::MAX_CONNECTIONS_REACHED_SELF;
4342
ExpiringTimeTracker _ongoingPeerRequestEncryptionTimeout([](){ return EspnowDatabase::getEncryptionRequestTimeout(); });
43+
uint8_t _ongoingPeerRequestMac[6] = {0};
4444
bool _reciprocalPeerRequestConfirmation = false;
4545
}
4646

@@ -465,7 +465,7 @@ bool EspnowEncryptionBroker::verifyEncryptionRequestHmac(const String &encryptio
465465
if(hmacStartIndex < 0)
466466
return false;
467467

468-
if(hmac.length() == 2*CryptoInterface::SHA256_NATURAL_LENGTH // We know that each HMAC byte should become 2 String characters due to uint8ArrayToHexString.
468+
if(hmac.length() == 2*experimental::crypto::SHA256::NATURAL_LENGTH // We know that each HMAC byte should become 2 String characters due to uint8ArrayToHexString.
469469
&& verifyMeshHmac(TypeCast::macToString(requesterStaMac) + TypeCast::macToString(requesterApMac) + encryptionRequestHmacMessage.substring(0, hmacStartIndex), hmac, hashKey, hashKeyLength))
470470
{
471471
return true;

0 commit comments

Comments
 (0)