From 85501622355a5b926c537c7b0aeb7770bc41848a Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Thu, 19 Feb 2015 11:22:36 +0100 Subject: [PATCH] Slim down dummy signing driver. --- libraries/MySensors/MySigningDriverDummy.cpp | 81 +------------------- libraries/MySensors/MySigningDriverDummy.h | 11 +-- 2 files changed, 6 insertions(+), 86 deletions(-) diff --git a/libraries/MySensors/MySigningDriverDummy.cpp b/libraries/MySensors/MySigningDriverDummy.cpp index 51d418f..f5bac3f 100644 --- a/libraries/MySensors/MySigningDriverDummy.cpp +++ b/libraries/MySensors/MySigningDriverDummy.cpp @@ -36,51 +36,17 @@ static void DEBUG_DUMMY_PRINTBUF(char* str, uint8_t* buf, uint8_t sz) #endif MySigningDriverDummy::MySigningDriverDummy() : MySigningDriver() { - verification_ongoing = false; - current_nonce = 0; } bool MySigningDriverDummy::getNonce(MyMessage &msg) { - uint8_t nonce_buf[2]; - nonce_buf[0] = SIGNING_IDENTIFIER; - // This is normally a buffer but for our minimal driver we use a single byte - // We at least use a new nonce for every session - nonce_buf[1] = ++current_nonce; - msg.set(nonce_buf, 2); - verification_ongoing = true; - timestamp = millis(); // Set timestamp to determine when to purge nonce - // Be a little fancy to handle turnover (prolong the time allowed to timeout after turnover) - // Note that if message is "too" quick, and arrives before turnover, it will be rejected - // but this is consider such a rare case that it is accepted and rejects are 'safe' - if (timestamp + VERIFICATION_TIMEOUT_MS < millis()) timestamp = 0; return true; } bool MySigningDriverDummy::checkTimer() { - if (verification_ongoing) { - if (millis() < timestamp || millis() > timestamp + VERIFICATION_TIMEOUT_MS) { - DEBUG_DUMMY_PRINTLN("Verification timeout"); - // Purge nonce - current_nonce = 0; // purge nonce when used - verification_ongoing = false; - return false; - } - } return true; } bool MySigningDriverDummy::putNonce(MyMessage &msg) { - if (mGetLength(msg) != 2) { - DEBUG_DUMMY_PRINTLN("Incoming nonce with incorrect size"); - return false; // We require a signing identifier and a 1byte nonce - } - - if (((uint8_t*)msg.getCustom())[0] != SIGNING_IDENTIFIER) { - DEBUG_DUMMY_PRINTLN("Incorrect signing identifier"); - return false; - } - - current_nonce = ((uint8_t*)msg.getCustom())[1]; return true; } @@ -90,54 +56,15 @@ bool MySigningDriverDummy::signMsg(MyMessage &msg) { DEBUG_DUMMY_PRINTLN("Cannot fit any signature to this message"); return false; } - - // Calculate signature of message mSetSigned(msg, 1); // make sure signing flag is set before signature is calculated - DEBUG_DUMMY_PRINTBUF("Message to sign:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg))); - uint8_t signature = msg.data[0] ^ current_nonce; msg.data[mGetLength(msg)] = SIGNING_IDENTIFIER; // Add signing identifier - msg.data[mGetLength(msg)+1] = signature; - current_nonce = 0; // purge nonce when used return true; } bool MySigningDriverDummy::verifyMsg(MyMessage &msg) { - if (!verification_ongoing) { - DEBUG_DUMMY_PRINTLN("No active verification session"); - return false; - } else { - // Make sure we have not expired - if (!checkTimer()) { - return false; - } - - verification_ongoing = false; - - if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) { - DEBUG_DUMMY_PRINTLN("Incorrect signing identifier"); - return false; - } - - DEBUG_DUMMY_PRINTBUF("Message to verify:", (uint8_t*)&msg.data[1-HEADER_SIZE], MAX_MESSAGE_LENGTH-1-(MAX_PAYLOAD-mGetLength(msg))); - DEBUG_DUMMY_PRINTBUF("Signature in message:", (uint8_t*)&msg.data[mGetLength(msg)], 2); - - // The dummy signing is the nonce XOR the first payload byte, - // so we "verify" by redoing that calculation with the nonce we transmitted - // Message without a payload this implementation always accepts as signed and messages that cannot - // fit a signature are also accepted as signed - if (mGetLength(msg) && mGetLength(msg) < MAX_PAYLOAD-2) { - uint8_t signature = msg.data[0] ^ current_nonce; - if (msg.data[mGetLength(msg)+1] != signature) { - DEBUG_DUMMY_PRINTBUF("Signature bad. Calculated signature:", &signature, 1); - return false; - } else { - DEBUG_DUMMY_PRINTLN("Signature ok"); - return true; - } - } else { - DEBUG_DUMMY_PRINTLN("No signature in message"); - return false; - } - } + if (msg.data[mGetLength(msg)] != SIGNING_IDENTIFIER) + return false; + else + return true; } #endif diff --git a/libraries/MySensors/MySigningDriverDummy.h b/libraries/MySensors/MySigningDriverDummy.h index 74b88a6..bf5ee81 100644 --- a/libraries/MySensors/MySigningDriverDummy.h +++ b/libraries/MySensors/MySigningDriverDummy.h @@ -10,11 +10,8 @@ #include "MySigningDriver.h" #include -// The dummy signing driver implements the most rudimentary form of signing -// No pre-shared key is used, nonce is simply incremeted each turn and signature -// is only calculated on the fist byte using data XOR nonce. -// This driver is intended for library debugging and sensor network integrity -// verification (signed messages will force the radio to transmit all 32 bytes of data). +// The dummy signing driver that can be used for nodes +// not requiring signing // It does check SIGNING_IDENTIFIER byte to avoid illegal mixing of signing back-ends in // the network (as seen by this node) and it does verify proper execution order on the API. // The dummy driver rejects all other back-ends. @@ -27,10 +24,6 @@ public: bool putNonce(MyMessage &msg); bool signMsg(MyMessage &msg); bool verifyMsg(MyMessage &msg); -private: - unsigned long timestamp; - bool verification_ongoing; - uint8_t current_nonce; }; #endif