#pragma once #include /** * see docs/software/crypto.md for details. * */ class CryptoEngine { protected: /** Our per packet nonce */ uint8_t nonce[16]; public: virtual ~CryptoEngine() {} /** * Set the key used for encrypt, decrypt. * * As a special case: If all bytes are zero, we assume _no encryption_ and send all data in cleartext. * * @param numBytes must be 16 (AES128), 32 (AES256) or 0 (no crypt) * @param bytes a _static_ buffer that will remain valid for the life of this crypto instance (i.e. this class will cache the * provided pointer) */ virtual void setKey(size_t numBytes, uint8_t *bytes); /** * Encrypt a packet * * @param bytes is updated in place */ virtual void encrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes); virtual void decrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes); protected: /** * Init our 128 bit nonce for a new packet * * The NONCE is constructed by concatenating (from MSB to LSB): * a 64 bit packet number (stored in little endian order) * a 32 bit sending node number (stored in little endian order) * a 32 bit block counter (starts at zero) */ void initNonce(uint32_t fromNode, uint64_t packetNum); }; extern CryptoEngine *crypto;