From c2a75d064e05e841ca386d1318a275cd658af882 Mon Sep 17 00:00:00 2001 From: Patrick Fallberg Date: Sat, 7 Feb 2015 00:19:13 +0100 Subject: [PATCH] Signing driver framework The signing driver framework resembles the radio driver framework in the sense that it relies on MyConfig.h for driver specific settings. This allows the constructor to be clean and the API less likely to need change. The API is designed with function pairs for sender and receiver of a signed message. The sender of a signed message uses putNonce() and signMsg() and the receiver of a signed message uses getNonce() and verifyMsg(). In addition, receiver is expected to call checkTimer() at regular intervals to make sure the message session does not time out. The program flow is that the sender transmit, after determining that the message needs signing, a request to the receiver for a nonce (I_GET_NONCE). The receiver calls getNonce() to obtain a nonce and transmits the nonce back to the sender (I_GET_NONCE_RESPONSE). The sender stores the received nonce in the driver with putNonce() and then calls signMsg() to sign the message to be transmitted. The receiver then calls verifyMsg() to authenticate the signed message using the nonce transmitted earlier. The flow is described below: ---------------- -------- ---------- ------------------ |senders driver| |sender| |receiver| |receivers driver| ---------------- -------- ---------- ------------------ | | I_GET_NONCE | getNonce() | | |------------------->|------------->| | | | | | setNonce() |I_GET_NONCE_RESPONSE| | |<-----------|<-------------------|<-------------| | signMsg() | | | |<-----------| | verifyMsg() | |----------->|------------------->|------------->| | | | OK / not OK | | | |<-------------| It is expected that all signing schemes is built upon this principle. --- libraries/MySensors/MySigningDriver.cpp | 4 ++ libraries/MySensors/MySigningDriver.h | 60 +++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 libraries/MySensors/MySigningDriver.cpp create mode 100644 libraries/MySensors/MySigningDriver.h diff --git a/libraries/MySensors/MySigningDriver.cpp b/libraries/MySensors/MySigningDriver.cpp new file mode 100644 index 0000000..b109a3f --- /dev/null +++ b/libraries/MySensors/MySigningDriver.cpp @@ -0,0 +1,4 @@ +#include "MySigningDriver.h" + +MySigningDriver::MySigningDriver() { +} diff --git a/libraries/MySensors/MySigningDriver.h b/libraries/MySensors/MySigningDriver.h new file mode 100644 index 0000000..28a75ea --- /dev/null +++ b/libraries/MySensors/MySigningDriver.h @@ -0,0 +1,60 @@ +/* + The MySigning driver provides a generic API for various signing backends to offer + signing of MySensors messages. + + Created by Patrick "Anticimex" Fallberg +*/ +#ifndef MySigningDriver_h +#define MySigningDriver_h + +#include +#include "MyMessage.h" + +class MySigningDriver +{ +public: + // MySigningDriver constructor. + // Different parameters would be needed depending on signing backend (e.g. pins connected etc.). + // Keeping these parameters as #define's in MyConfig to streamline the driver interface. + MySigningDriver(); + + // Stores signing identifier and a new nonce in provided message for signing operations. + // All space in message payload buffer is used for signing identifier and nonce. + // Returns false if subsystem is busy processing an ongoing signing operation. + // If successful, this marks the start of a signing operation at the receiving side so + // implementation is expected to do any necessary initializations within this call. + virtual bool getNonce(MyMessage &msg) = 0; + + // Check timeout of verification session. + // Nonce will be purged if it takes too long for a signed message to be sent to the receiver. + // This function should be called on regular intervals. + virtual bool checkTimer(void) = 0; + + // Get nonce from provided message and store for signing operations. + // Returns false if subsystem is busy processing an ongoing signing operation. + // Returns false if signing identifier found in message is not supported by the used backend. + // If successful, this marks the start of a signing operation at the sending side so + // implementation is expected to do any necessary initializations within this call. + virtual bool putNonce(MyMessage &msg) = 0; + + // Signs provided message. All remaining space in message payload buffer is used for signing + // identifier and signature. + // Nonce used for signature calculation is the one generated previously using getNonce(). + // Nonce will be cleared when this function is called to prevent re-use of nonce. + // Returns false if subsystem is busy processing an ongoing signing operation. + // Returns false if not two bytes or more of free payload space is left in provided message. + // This ends a signing operation at the sending side so implementation is expected to do any + // deinitializations and enter a power saving state within this call. + virtual bool signMsg(MyMessage &msg) = 0; + + // Verifies signature in provided message. + // Nonce used for verification is the one previously set using putNonce(). + // Nonce will be cleared when this function is called to prevent re-use of nonce. + // Returns false if subsystem is busy processing an ongoing signing operation. + // Returns false if signing identifier found in message is not supported by the used backend. + // This ends a signing operation at the receiving side so implementation is expected to do any + // deinitializations and enter a power saving state within this call. + virtual bool verifyMsg(MyMessage &msg) = 0; +}; + +#endif