2022-08-15 07:54:45 -05:00
|
|
|
#include "CryptoEngine.h"
|
2023-11-23 13:47:07 +01:00
|
|
|
#include "aes.hpp"
|
2023-01-21 14:34:29 +01:00
|
|
|
#include "configuration.h"
|
2022-08-15 07:54:45 -05:00
|
|
|
|
|
|
|
|
class STM32WLCryptoEngine : public CryptoEngine
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
STM32WLCryptoEngine() {}
|
|
|
|
|
|
|
|
|
|
~STM32WLCryptoEngine() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encrypt a packet
|
|
|
|
|
*
|
|
|
|
|
* @param bytes is updated in place
|
|
|
|
|
*/
|
2023-11-23 13:47:07 +01:00
|
|
|
virtual void encrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes) override
|
2022-08-15 07:54:45 -05:00
|
|
|
{
|
|
|
|
|
if (key.length > 0) {
|
2023-11-23 13:47:07 +01:00
|
|
|
AES_ctx ctx;
|
|
|
|
|
initNonce(fromNode, packetNum);
|
|
|
|
|
AES_init_ctx_iv(&ctx, key.bytes, nonce);
|
|
|
|
|
AES_CTR_xcrypt_buffer(&ctx, bytes, numBytes);
|
2022-08-15 07:54:45 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 13:47:07 +01:00
|
|
|
virtual void decrypt(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes) override
|
2022-08-15 07:54:45 -05:00
|
|
|
{
|
|
|
|
|
// For CTR, the implementation is the same
|
2023-11-23 13:47:07 +01:00
|
|
|
encrypt(fromNode, packetNum, numBytes, bytes);
|
2022-08-15 07:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CryptoEngine *crypto = new STM32WLCryptoEngine();
|