2020-05-24 14:15:49 -07:00
|
|
|
#include "configuration.h"
|
2021-06-27 10:56:28 -07:00
|
|
|
#include "CryptoEngine.h"
|
2020-05-24 14:15:49 -07:00
|
|
|
#include "ocrypto_aes_ctr.h"
|
2020-05-09 21:02:56 -07:00
|
|
|
|
2020-05-24 14:15:49 -07:00
|
|
|
class NRF52CryptoEngine : public CryptoEngine
|
|
|
|
|
{
|
|
|
|
|
|
2021-02-23 10:10:35 +08:00
|
|
|
|
2020-05-24 14:15:49 -07:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
NRF52CryptoEngine() {}
|
|
|
|
|
|
|
|
|
|
~NRF52CryptoEngine() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encrypt a packet
|
|
|
|
|
*
|
|
|
|
|
* @param bytes is updated in place
|
|
|
|
|
*/
|
2022-03-20 11:40:13 +11:00
|
|
|
virtual void encrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override
|
2020-05-24 14:15:49 -07:00
|
|
|
{
|
|
|
|
|
// DEBUG_MSG("NRF52 encrypt!\n");
|
|
|
|
|
|
2021-02-23 10:10:35 +08:00
|
|
|
if (key.length > 0) {
|
2020-05-24 14:15:49 -07:00
|
|
|
ocrypto_aes_ctr_ctx ctx;
|
|
|
|
|
|
2022-03-20 11:40:13 +11:00
|
|
|
initNonce(fromNode, packetId);
|
2021-02-23 10:10:35 +08:00
|
|
|
ocrypto_aes_ctr_init(&ctx, key.bytes, key.length, nonce);
|
2020-05-24 14:15:49 -07:00
|
|
|
|
|
|
|
|
ocrypto_aes_ctr_encrypt(&ctx, bytes, bytes, numBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-20 11:40:13 +11:00
|
|
|
virtual void decrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override
|
2020-05-24 14:15:49 -07:00
|
|
|
{
|
|
|
|
|
// DEBUG_MSG("NRF52 decrypt!\n");
|
|
|
|
|
|
2021-02-23 10:10:35 +08:00
|
|
|
if (key.length > 0) {
|
2020-05-24 14:15:49 -07:00
|
|
|
ocrypto_aes_ctr_ctx ctx;
|
|
|
|
|
|
2022-03-20 11:40:13 +11:00
|
|
|
initNonce(fromNode, packetId);
|
2021-02-23 10:10:35 +08:00
|
|
|
ocrypto_aes_ctr_init(&ctx, key.bytes, key.length, nonce);
|
2020-05-24 14:15:49 -07:00
|
|
|
|
|
|
|
|
ocrypto_aes_ctr_decrypt(&ctx, bytes, bytes, numBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CryptoEngine *crypto = new NRF52CryptoEngine();
|