First working seqno sync with ETS

This commit is contained in:
Nanosonde 2020-06-22 22:01:20 +02:00
parent 99918eb08a
commit abba1929f6
2 changed files with 6 additions and 7 deletions

View File

@ -392,7 +392,6 @@ void SecureApplicationLayer::sixBytesFromUInt64(uint64_t num, uint8_t* toByteArr
uint64_t SecureApplicationLayer::sixBytesToUInt64(uint8_t* data) uint64_t SecureApplicationLayer::sixBytesToUInt64(uint8_t* data)
{ {
/*
uint64_t l = 0; uint64_t l = 0;
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
@ -400,9 +399,6 @@ uint64_t SecureApplicationLayer::sixBytesToUInt64(uint8_t* data)
l = (l << 8) + data[i]; l = (l << 8) + data[i];
} }
return l; return l;
*/
return ((uint64_t)data[0] << 40) + ((uint64_t)data[1] << 32) +
(data[2] << 24) + (data[3] << 16) + (data[4] << 8) + data[5];
} }
const uint8_t* SecureApplicationLayer::toolKey(uint16_t devAddr) const uint8_t* SecureApplicationLayer::toolKey(uint16_t devAddr)
@ -543,7 +539,8 @@ void SecureApplicationLayer::sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGr
sixBytesFromUInt64(ourNextSeqNum, &asdu[0]); sixBytesFromUInt64(ourNextSeqNum, &asdu[0]);
sixBytesFromUInt64(remoteNextSeqNum, &asdu[6]); sixBytesFromUInt64(remoteNextSeqNum, &asdu[6]);
CemiFrame response(3 + 6 + sizeof(asdu) + 4); // 3 bytes (TPCI, APCI, SCF) + 6 bytes (SeqNum) + 12 bytes + 4 bytes (MAC) CemiFrame response(2 + 6 + sizeof(asdu) + 4); // 2 bytes (APCI, SCF) + 6 bytes (SeqNum) + 12 bytes + 4 bytes (MAC)
// Note: additional TPCI byte is already handled internally!
uint8_t tpci = 0; uint8_t tpci = 0;
if (!_syncReqBroadcast) if (!_syncReqBroadcast)
@ -574,7 +571,7 @@ void SecureApplicationLayer::sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGr
} }
} }
void SecureApplicationLayer::receivedSyncRequest(uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint8_t* seqNum, long challenge) void SecureApplicationLayer::receivedSyncRequest(uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint8_t* seqNum, uint64_t challenge)
{ {
uint64_t nextRemoteSeqNum = sixBytesToUInt64(seqNum); uint64_t nextRemoteSeqNum = sixBytesToUInt64(seqNum);
uint64_t nextSeqNum = 1 + lastValidSequenceNumber(toolAccess, srcAddr); uint64_t nextSeqNum = 1 + lastValidSequenceNumber(toolAccess, srcAddr);
@ -964,6 +961,8 @@ bool SecureApplicationLayer::secure(uint8_t* buffer, uint16_t service, uint16_t
println("sending sync.res without corresponding .req"); println("sending sync.res without corresponding .req");
} }
printHex("Decrypted challenge: ", _challenge, 6);
// Now XOR the new random SeqNum with the challenge from the SyncRequest // Now XOR the new random SeqNum with the challenge from the SyncRequest
uint8_t rndXorChallenge[6]; uint8_t rndXorChallenge[6];
pushByteArray(seq, 6, rndXorChallenge); pushByteArray(seq, 6, rndXorChallenge);

View File

@ -81,7 +81,7 @@ class SecureApplicationLayer : public ApplicationLayer
void updateLastValidSequence(bool toolAccess, uint16_t remoteAddr, uint64_t seqNo); void updateLastValidSequence(bool toolAccess, uint16_t remoteAddr, uint64_t seqNo);
void sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint64_t remoteNextSeqNum); void sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint64_t remoteNextSeqNum);
void receivedSyncRequest(uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint8_t* seq, long challenge); void receivedSyncRequest(uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, bool toolAccess, uint8_t* seq, uint64_t challenge);
void receivedSyncResponse(uint16_t remoteAddr, bool toolAccess, uint8_t* plainApdu); void receivedSyncResponse(uint16_t remoteAddr, bool toolAccess, uint8_t* plainApdu);
bool decrypt(uint8_t* plainApdu, uint16_t plainapduLength, uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, uint8_t tpci, uint8_t* secureAsdu); bool decrypt(uint8_t* plainApdu, uint16_t plainapduLength, uint16_t srcAddr, uint16_t dstAddr, bool dstAddrIsGroupAddr, uint8_t tpci, uint8_t* secureAsdu);