RadioHead
RHEncryptedDriver.h
1// RHEncryptedDriver.h
2
3// Generic encryption layer that could use any driver
4// But will encrypt all data.
5// Requires the Arduinolibs/Crypto library:
6// https://github.com/rweather/arduinolibs
7//
8// Author: Philippe.Rochat'at'gmail.com
9// Contributed to the RadioHead project by the author
10// $Id: RHEncryptedDriver.h,v 1.4 2020/07/05 08:52:21 mikem Exp $
11
12#ifndef RHEncryptedDriver_h
13#define RHEncryptedDriver_h
14
15#include <RHGenericDriver.h>
16#if defined(RH_ENABLE_ENCRYPTION_MODULE) || defined(DOXYGEN)
17#include <BlockCipher.h>
18
19// Undef this if trailing 0 on each enrypted message is ok.
20// This defined means a first byte of the payload is used to encode content length
21// And the received message content is trimmed to this length
22#define STRICT_CONTENT_LEN
23
24// Define this to allow encrypted content to span over 2 messages
25// STRICT_CONTENT_LEN and ALLOW_MULTIPLE_MSG aren't compatible !!!
26// With STRICT_CONTENT_LEN, receiver will try to extract length from every message !!!!
27//#define ALLOW_MULTIPLE_MSG
28
29/////////////////////////////////////////////////////////////////////
30/// \class RHEncryptedDriver RHEncryptedDriver <RHEncryptedDriver.h>
31/// \brief Virtual Driver to encrypt/decrypt data. Can be used with any other RadioHead driver.
32///
33/// This driver acts as a wrapper for any other RadioHead driver, adding encryption and decryption of
34/// messages that are passed to and from the actual radio driver. Only the message payload is encrypted,
35/// and not the to/from address or flags. Any of the encryption ciphers supported by
36/// ArduinoLibs Cryptographic Library http://rweather.github.io/arduinolibs/crypto.html may be used.
37///
38/// For successful communications, both sender and receiver must use the same cipher and the same key.
39///
40/// In order to enable this module you must uncomment #define RH_ENABLE_ENCRYPTION_MODULE at the bottom of RadioHead.h
41/// But ensure you have installed the Crypto directory from arduinolibs first:
42/// http://rweather.github.io/arduinolibs/index.html
43
45{
46public:
47 /// Constructor.
48 /// Adds a ciphering layer to messages sent and received by the actual transport driver.
49 /// \param[in] driver The RadioHead driver to use to transport messages.
50 /// \param[in] blockcipher The blockcipher (from arduinolibs) that crypt/decrypt data. Ensure that
51 /// the blockcipher has had its key set before sending or receiving messages.
52 RHEncryptedDriver(RHGenericDriver& driver, BlockCipher& blockcipher);
53
54 /// Calls the real driver's init()
55 /// \return The value returned from the driver init() method;
56 virtual bool init() { return _driver.init();};
57
58 /// Tests whether a new message is available
59 /// from the Driver.
60 /// On most drivers, this will also put the Driver into RHModeRx mode until
61 /// a message is actually received by the transport, when it wil be returned to RHModeIdle.
62 /// This can be called multiple times in a timeout loop
63 /// \return true if a new, complete, error-free uncollected message is available to be retreived by recv()
64 virtual bool available() { return _driver.available();};
65
66 /// Turns the receiver on if it not already on.
67 /// If there is a valid message available, copy it to buf and return true
68 /// else return false.
69 /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted).
70 /// You should be sure to call this function frequently enough to not miss any messages
71 /// It is recommended that you call it in your main loop.
72 /// \param[in] buf Location to copy the received message
73 /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied.
74 /// \return true if a valid message was copied to buf
75 virtual bool recv(uint8_t* buf, uint8_t* len);
76
77 /// Waits until any previous transmit packet is finished being transmitted with waitPacketSent().
78 /// Then optionally waits for Channel Activity Detection (CAD)
79 /// to show the channnel is clear (if the radio supports CAD) by calling waitCAD().
80 /// Then loads a message into the transmitter and starts the transmitter. Note that a message length
81 /// of 0 is permitted.
82 /// \param[in] data Array of data to be sent
83 /// \param[in] len Number of bytes of data to send
84 /// specify the maximum time in ms to wait. If 0 (the default) do not wait for CAD before transmitting.
85 /// \return true if the message length was valid and it was correctly queued for transmit. Return false
86 /// if CAD was requested and the CAD timeout timed out before clear channel was detected.
87 virtual bool send(const uint8_t* data, uint8_t len);
88
89 /// Returns the maximum message length
90 /// available in this Driver, which depends on the maximum length supported by the underlying transport driver.
91 /// \return The maximum legal message length
92 virtual uint8_t maxMessageLength();
93
94 /// Blocks until the transmitter
95 /// is no longer transmitting.
96 virtual bool waitPacketSent() { return _driver.waitPacketSent();} ;
97
98 /// Blocks until the transmitter is no longer transmitting.
99 /// or until the timeout occuers, whichever happens first
100 /// \param[in] timeout Maximum time to wait in milliseconds.
101 /// \return true if the radio completed transmission within the timeout period. False if it timed out.
102 virtual bool waitPacketSent(uint16_t timeout) {return _driver.waitPacketSent(timeout);} ;
103
104 /// Starts the receiver and blocks until a received message is available or a timeout
105 /// \param[in] timeout Maximum time to wait in milliseconds.
106 /// \return true if a message is available
107 virtual bool waitAvailableTimeout(uint16_t timeout) {return _driver.waitAvailableTimeout(timeout);};
108
109 /// Calls the waitCAD method in the driver
110 /// \return The return value from teh drivers waitCAD() method
111 virtual bool waitCAD() { return _driver.waitCAD();};
112
113 /// Sets the Channel Activity Detection timeout in milliseconds to be used by waitCAD().
114 /// The default is 0, which means do not wait for CAD detection.
115 /// CAD detection depends on support for isChannelActive() by your particular radio.
116 void setCADTimeout(unsigned long cad_timeout) {_driver.setCADTimeout(cad_timeout);};
117
118 /// Determine if the currently selected radio channel is active.
119 /// This is expected to be subclassed by specific radios to implement their Channel Activity Detection
120 /// if supported. If the radio does not support CAD, returns true immediately. If a RadioHead radio
121 /// supports isChannelActive() it will be documented in the radio specific documentation.
122 /// This is called automatically by waitCAD().
123 /// \return true if the radio-specific CAD (as returned by override of isChannelActive()) shows the
124 /// current radio channel as active, else false. If there is no radio-specific CAD, returns false.
125 virtual bool isChannelActive() { return _driver.isChannelActive();};
126
127 /// Sets the address of this node. Defaults to 0xFF. Subclasses or the user may want to change this.
128 /// This will be used to test the adddress in incoming messages. In non-promiscuous mode,
129 /// only messages with a TO header the same as thisAddress or the broadcast addess (0xFF) will be accepted.
130 /// In promiscuous mode, all messages will be accepted regardless of the TO header.
131 /// In a conventional multinode system, all nodes will have a unique address
132 /// (which you could store in EEPROM).
133 /// You would normally set the header FROM address to be the same as thisAddress (though you dont have to,
134 /// allowing the possibilty of address spoofing).
135 /// \param[in] thisAddress The address of this node.
136 virtual void setThisAddress(uint8_t thisAddress) { _driver.setThisAddress(thisAddress);};
137
138 /// Sets the TO header to be sent in all subsequent messages
139 /// \param[in] to The new TO header value
140 virtual void setHeaderTo(uint8_t to){ _driver.setHeaderTo(to);};
141
142 /// Sets the FROM header to be sent in all subsequent messages
143 /// \param[in] from The new FROM header value
144 virtual void setHeaderFrom(uint8_t from){ _driver.setHeaderFrom(from);};
145
146 /// Sets the ID header to be sent in all subsequent messages
147 /// \param[in] id The new ID header value
148 virtual void setHeaderId(uint8_t id){ _driver.setHeaderId(id);};
149
150 /// Sets and clears bits in the FLAGS header to be sent in all subsequent messages
151 /// First it clears he FLAGS according to the clear argument, then sets the flags according to the
152 /// set argument. The default for clear always clears the application specific flags.
153 /// \param[in] set bitmask of bits to be set. Flags are cleared with the clear mask before being set.
154 /// \param[in] clear bitmask of flags to clear. Defaults to RH_FLAGS_APPLICATION_SPECIFIC
155 /// which clears the application specific flags, resulting in new application specific flags
156 /// identical to the set.
157 virtual void setHeaderFlags(uint8_t set, uint8_t clear = RH_FLAGS_APPLICATION_SPECIFIC) { _driver.setHeaderFlags(set, clear);};
158
159 /// Tells the receiver to accept messages with any TO address, not just messages
160 /// addressed to thisAddress or the broadcast address
161 /// \param[in] promiscuous true if you wish to receive messages with any TO address
162 virtual void setPromiscuous(bool promiscuous){ _driver.setPromiscuous(promiscuous);};
163
164 /// Returns the TO header of the last received message
165 /// \return The TO header
166 virtual uint8_t headerTo() { return _driver.headerTo();};
167
168 /// Returns the FROM header of the last received message
169 /// \return The FROM header
170 virtual uint8_t headerFrom() { return _driver.headerFrom();};
171
172 /// Returns the ID header of the last received message
173 /// \return The ID header
174 virtual uint8_t headerId() { return _driver.headerId();};
175
176 /// Returns the FLAGS header of the last received message
177 /// \return The FLAGS header
178 virtual uint8_t headerFlags() { return _driver.headerFlags();};
179
180 /// Returns the most recent RSSI (Receiver Signal Strength Indicator).
181 /// Usually it is the RSSI of the last received message, which is measured when the preamble is received.
182 /// If you called readRssi() more recently, it will return that more recent value.
183 /// \return The most recent RSSI measurement in dBm.
184 int16_t lastRssi() { return _driver.lastRssi();};
185
186 /// Returns the operating mode of the library.
187 /// \return the current mode, one of RF69_MODE_*
188 RHMode mode() { return _driver.mode();};
189
190 /// Sets the operating mode of the transport.
191 void setMode(RHMode mode) { _driver.setMode(mode);};
192
193 /// Sets the transport hardware into low-power sleep mode
194 /// (if supported). May be overridden by specific drivers to initialte sleep mode.
195 /// If successful, the transport will stay in sleep mode until woken by
196 /// changing mode it idle, transmit or receive (eg by calling send(), recv(), available() etc)
197 /// \return true if sleep mode is supported by transport hardware and the RadioHead driver, and if sleep mode
198 /// was successfully entered. If sleep mode is not suported, return false.
199 virtual bool sleep() { return _driver.sleep();};
200
201 /// Returns the count of the number of bad received packets (ie packets with bad lengths, checksum etc)
202 /// which were rejected and not delivered to the application.
203 /// Caution: not all drivers can correctly report this count. Some underlying hardware only report
204 /// good packets.
205 /// \return The number of bad packets received.
206 virtual uint16_t rxBad() { return _driver.rxBad();};
207
208 /// Returns the count of the number of
209 /// good received packets
210 /// \return The number of good packets received.
211 virtual uint16_t rxGood() { return _driver.rxGood();};
212
213 /// Returns the count of the number of
214 /// packets successfully transmitted (though not necessarily received by the destination)
215 /// \return The number of packets successfully transmitted
216 virtual uint16_t txGood() { return _driver.txGood();};
217
218private:
219 /// The underlying transport river we are to use
220 RHGenericDriver& _driver;
221
222 /// The CipherBlock we are to use for encrypting/decrypting
223 BlockCipher& _blockcipher;
224
225 /// Struct for with buffers for ciphering
226 typedef struct
227 {
228 size_t blockSize = 0;
229 uint8_t *inputBlock = NULL;
230 //uint8_t *outputBlock = NULL;
231 } CipherBlocks;
232
233 CipherBlocks _cipheringBlocks;
234
235 /// Buffer to store encrypted/decrypted message
236 uint8_t* _buffer;
237};
238
239/// @example nrf24_encrypted_client.ino
240/// @example nrf24_encrypted_server.ino
241/// @example rf95_encrypted_client.ino
242/// @example rf95_encrypted_server.ino
243/// @example serial_encrypted_reliable_datagram_client.ino
244/// @example serial_encrypted_reliable_datagram_server.ino
245
246
247#else // RH_ENABLE_ENCRYPTION_MODULE
248#error "You have included RHEncryptedDriver.h, but not enabled RH_ENABLE_ENCRYPTION_MODULE in RadioHead.h"
249#endif
250
251#endif
Virtual Driver to encrypt/decrypt data. Can be used with any other RadioHead driver.
Definition: RHEncryptedDriver.h:45
virtual uint16_t txGood()
Definition: RHEncryptedDriver.h:216
void setCADTimeout(unsigned long cad_timeout)
Definition: RHEncryptedDriver.h:116
virtual uint8_t headerId()
Definition: RHEncryptedDriver.h:174
virtual void setPromiscuous(bool promiscuous)
Definition: RHEncryptedDriver.h:162
int16_t lastRssi()
Definition: RHEncryptedDriver.h:184
virtual bool send(const uint8_t *data, uint8_t len)
virtual bool sleep()
Definition: RHEncryptedDriver.h:199
RHMode mode()
Definition: RHEncryptedDriver.h:188
virtual bool available()
Definition: RHEncryptedDriver.h:64
virtual uint8_t maxMessageLength()
virtual bool recv(uint8_t *buf, uint8_t *len)
virtual bool waitCAD()
Definition: RHEncryptedDriver.h:111
RHEncryptedDriver(RHGenericDriver &driver, BlockCipher &blockcipher)
void setMode(RHMode mode)
Sets the operating mode of the transport.
Definition: RHEncryptedDriver.h:191
virtual uint8_t headerTo()
Definition: RHEncryptedDriver.h:166
virtual bool waitAvailableTimeout(uint16_t timeout)
Definition: RHEncryptedDriver.h:107
virtual void setHeaderFlags(uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC)
Definition: RHEncryptedDriver.h:157
virtual uint16_t rxGood()
Definition: RHEncryptedDriver.h:211
virtual uint16_t rxBad()
Definition: RHEncryptedDriver.h:206
virtual uint8_t headerFlags()
Definition: RHEncryptedDriver.h:178
virtual uint8_t headerFrom()
Definition: RHEncryptedDriver.h:170
virtual void setHeaderTo(uint8_t to)
Definition: RHEncryptedDriver.h:140
virtual void setHeaderId(uint8_t id)
Definition: RHEncryptedDriver.h:148
virtual void setThisAddress(uint8_t thisAddress)
Definition: RHEncryptedDriver.h:136
virtual void setHeaderFrom(uint8_t from)
Definition: RHEncryptedDriver.h:144
virtual bool isChannelActive()
Definition: RHEncryptedDriver.h:125
virtual bool waitPacketSent(uint16_t timeout)
Definition: RHEncryptedDriver.h:102
virtual bool waitPacketSent()
Definition: RHEncryptedDriver.h:96
virtual bool init()
Definition: RHEncryptedDriver.h:56
Abstract base class for a RadioHead driver.
Definition: RHGenericDriver.h:42
virtual uint16_t rxBad()
Definition: RHGenericDriver.cpp:200
virtual uint8_t headerFlags()
Definition: RHGenericDriver.cpp:155
virtual void setHeaderTo(uint8_t to)
Definition: RHGenericDriver.cpp:119
virtual void setPromiscuous(bool promiscuous)
Definition: RHGenericDriver.cpp:109
virtual bool available()=0
virtual bool init()
Definition: RHGenericDriver.cpp:23
void setCADTimeout(unsigned long cad_timeout)
Definition: RHGenericDriver.cpp:215
virtual void setHeaderFrom(uint8_t from)
Definition: RHGenericDriver.cpp:124
virtual void setHeaderId(uint8_t id)
Definition: RHGenericDriver.cpp:129
virtual void setThisAddress(uint8_t thisAddress)
Definition: RHGenericDriver.cpp:114
virtual uint16_t rxGood()
Definition: RHGenericDriver.cpp:205
virtual uint16_t txGood()
Definition: RHGenericDriver.cpp:210
virtual uint8_t headerTo()
Definition: RHGenericDriver.cpp:140
virtual bool waitPacketSent()
Definition: RHGenericDriver.cpp:58
RHMode
Defines different operating modes for the transport hardware.
Definition: RHGenericDriver.h:49
virtual RHMode mode()
Definition: RHGenericDriver.cpp:165
virtual uint8_t headerId()
Definition: RHGenericDriver.cpp:150
virtual uint8_t headerFrom()
Definition: RHGenericDriver.cpp:145
virtual bool waitAvailableTimeout(uint16_t timeout, uint16_t polldelay=0)
Definition: RHGenericDriver.cpp:42
virtual bool sleep()
Definition: RHGenericDriver.cpp:175
virtual void setMode(RHMode mode)
Sets the operating mode of the transport.
Definition: RHGenericDriver.cpp:170
virtual int16_t lastRssi()
Definition: RHGenericDriver.cpp:160
virtual bool waitCAD()
Definition: RHGenericDriver.cpp:78
virtual void setHeaderFlags(uint8_t set, uint8_t clear=RH_FLAGS_APPLICATION_SPECIFIC)
Definition: RHGenericDriver.cpp:134
virtual bool isChannelActive()
Definition: RHGenericDriver.cpp:104