2021-06-27 10:56:28 -07:00
|
|
|
#include "CryptoEngine.h"
|
2022-04-26 17:50:50 +02:00
|
|
|
#include "aes-256/tiny-aes.h"
|
2023-01-21 14:34:29 +01:00
|
|
|
#include "configuration.h"
|
|
|
|
|
#include <Adafruit_nRFCrypto.h>
|
2020-05-24 14:15:49 -07:00
|
|
|
class NRF52CryptoEngine : public CryptoEngine
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
NRF52CryptoEngine() {}
|
|
|
|
|
|
|
|
|
|
~NRF52CryptoEngine() {}
|
|
|
|
|
|
2024-08-10 22:38:05 -05:00
|
|
|
virtual void encryptAESCtr(CryptoKey _key, uint8_t *_nonce, size_t numBytes, uint8_t *bytes) override
|
2020-05-24 14:15:49 -07:00
|
|
|
{
|
2024-08-10 22:38:05 -05:00
|
|
|
if (_key.length > 16) {
|
2022-04-26 17:50:50 +02:00
|
|
|
AES_ctx ctx;
|
2024-08-10 22:38:05 -05:00
|
|
|
AES_init_ctx_iv(&ctx, _key.bytes, _nonce);
|
2022-04-26 17:50:50 +02:00
|
|
|
AES_CTR_xcrypt_buffer(&ctx, bytes, numBytes);
|
2024-08-10 22:38:05 -05:00
|
|
|
} else if (_key.length > 0) {
|
2022-04-25 11:01:54 +02:00
|
|
|
nRFCrypto.begin();
|
|
|
|
|
nRFCrypto_AES ctx;
|
|
|
|
|
uint8_t myLen = ctx.blockLen(numBytes);
|
|
|
|
|
char encBuf[myLen] = {0};
|
|
|
|
|
ctx.begin();
|
2024-08-10 22:38:05 -05:00
|
|
|
ctx.Process((char *)bytes, numBytes, _nonce, _key.bytes, _key.length, encBuf, ctx.encryptFlag, ctx.ctrMode);
|
2022-04-25 11:01:54 +02:00
|
|
|
ctx.end();
|
|
|
|
|
nRFCrypto.end();
|
2022-06-04 10:37:24 +02:00
|
|
|
memcpy(bytes, encBuf, numBytes);
|
2020-05-24 14:15:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-10 22:38:05 -05:00
|
|
|
CryptoEngine *crypto = new NRF52CryptoEngine();
|