rf12_encrypt()

This enables or disables encryption using the public domain XXTEA algorithm by David Wheeler. The payload will be extended with 1 .. 4 bytes, containing a 6..30-bit sequence number which is incremented in the sender for each new packet.

The number of bits sent across depends on the number of padding bytes needed to make the resulting payload an exact mulitple of 4 bytes. A longer sequence number field can provide more protection against replay attacks (note that verification of this sequence number must be implemented in the receiver code).

Encrypted packets (and acknowledgements) must be 4..62 bytes long. Packets less than 4 bytes will not be encrypted. On reception, the payload length is adjusted back to the original length passed to rf12_sendStart().

There is a "long rf12seq" global which is set to the received sequence number (only valid right after rf12recvDone() returns true). When encryption is not enabled, this global is set to -1.

#include <RF12.h> 
#include <Ports.h> // needed to avoid a linker error :(

void rf12_encrypt(const uint8_t* key);

Parameters

key

Pointer to a 16-byte (128-bit) encryption key to use for all packet data. A null pointer disables encryption again. Note: this is an EEPROM address, not RAM! - RF12_EEPROM_EKEY is a great value to use, as defined in the include file, but another address can be specified if needed.

See Also

There is a weblog post at http://jeelabs.org/2010/02/23/secure-transmissions/ with an example.