Skip to content

Commit 03e06d1

Browse files
committed
#217 in progress support for encryption
1 parent 28687f8 commit 03e06d1

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

examples/mbed/stm32OledEncoder/generated/MBedEthernetTransport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace tcremote {
3131
InternetSocket* socket;
3232
bool isOpen;
3333
public:
34-
MBedEthernetTransport() : BaseBufferedRemoteTransport(BUFFER_MESSAGES_TILL_FULL, 96, 250) {
34+
MBedEthernetTransport(EncryptionHandler* encHandler) : BaseBufferedRemoteTransport(BUFFER_ONE_MESSAGE, 250, 250, encHandler) {
3535
this->socket = nullptr;
3636
isOpen = false;
3737
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "MbedTlsEncryptionHandler.h"
2+
3+
using namespace tcremote;
4+
5+
MbedTlsEncryptionHandler::MbedTlsEncryptionHandler(const uint8_t* keyIn) {
6+
memcpy(key, keyIn, sizeof key);
7+
memset(ivDec, 0, sizeof ivDec);
8+
memset(ivEnc, 0, sizeof ivEnc);
9+
}
10+
11+
void MbedTlsEncryptionHandler::initialise() {
12+
// initialise random number generation and aes.
13+
mbedtls_aes_init(&aes);
14+
mbedtls_entropy_init( &entropy );
15+
mbedtls_ctr_drbg_init( &ctr_drbg );
16+
17+
// create a reasonably random seed, improved if you define TCMENU_ENTROPY_CUSTOM_DATA yourself
18+
mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
19+
(unsigned char *) TCMENU_ENTROPY_CUSTOM_DATA,
20+
strlen( TCMENU_ENTROPY_CUSTOM_DATA ));
21+
22+
// randomize the initialisation vectors
23+
mbedtls_ctr_drbg_random(&ctr_drbg, ivDec, sizeof ivDec);
24+
mbedtls_ctr_drbg_random(&ctr_drbg, ivEnc, sizeof ivEnc);
25+
}
26+
27+
size_t roundToNearest(int in) {
28+
// we need to send in increments of 16 bytes, so we autofill to that boundary.
29+
auto remainder = in % 16;
30+
if(remainder == 0) return in;
31+
return in + (16-remainder);
32+
}
33+
34+
int MbedTlsEncryptionHandler::encryptData(uint8_t *plainText, int bytesIn, uint8_t *buffer, size_t buffLen) {
35+
mbedtls_aes_setkey_enc(&aes, key, 256);
36+
// find the nearest 16 byte boundary that is larger than bytesIn.
37+
size_t totalIn = roundToNearest(bytesIn);
38+
// ensure the buffers are big enough and then fill the end of the buffer with zeros.
39+
if(totalIn + 16 >= buffLen) return 0;
40+
for(size_t i=bytesIn; i<totalIn;i++) {
41+
plainText[i] = 0;
42+
}
43+
// then encrypt the data
44+
memcpy(buffer, ivEnc, 16);
45+
bool ok = mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_ENCRYPT, totalIn, ivEnc, plainText, &buffer[16]) == 0;
46+
return ok ? (int)totalIn : 0;
47+
48+
}
49+
50+
int MbedTlsEncryptionHandler::decryptData(const uint8_t *encoded, int bytesIn, uint8_t *buffer, size_t buffLen) {
51+
if(bytesIn < 32 || buffLen < bytesIn - 16) return 0;
52+
memcpy(ivDec, encoded, 16);
53+
mbedtls_aes_setkey_dec(&aes, key, 256);
54+
bool ok = mbedtls_aes_crypt_cbc(&aes, MBEDTLS_AES_DECRYPT, bytesIn, ivDec, &encoded[16], buffer) == 0;
55+
return ok ? bytesIn : 0;
56+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by dave on 10/06/2024.
3+
//
4+
5+
#ifndef TCCLIBSDK_MBEDTLSENCRYPTIONHANDLER_H
6+
#define TCCLIBSDK_MBEDTLSENCRYPTIONHANDLER_H
7+
8+
#include "remote/BaseBufferedRemoteTransport.h"
9+
#include <mbedtls/aes.h>
10+
#include <mbedtls/ctr_drbg.h>
11+
#include "mbedtls/entropy.h"
12+
13+
#ifndef TCMENU_ENTROPY_CUSTOM_DATA
14+
#define TCMENU_ENTROPY_CUSTOM_DATA "TcMenu Custom Entropy Str"
15+
#endif //TCMENU_ENTROPY_CUSTOM_DATA
16+
17+
namespace tcremote {
18+
19+
class MbedTlsEncryptionHandler : public EncryptionHandler {
20+
private:
21+
mbedtls_entropy_context entropy;
22+
mbedtls_aes_context aes;
23+
mbedtls_ctr_drbg_context ctr_drbg;
24+
unsigned char key[32];
25+
unsigned char ivEnc[16];
26+
unsigned char ivDec[16];
27+
public:
28+
explicit MbedTlsEncryptionHandler(const uint8_t* keyIn);
29+
void initialise();
30+
int encryptData(uint8_t *plainText, int bytesIn, uint8_t *buffer, size_t buffLen) override;
31+
int decryptData(const uint8_t *encoded, int bytesIn, uint8_t *buffer, size_t buffLen) override;
32+
};
33+
34+
}
35+
36+
#endif //TCCLIBSDK_MBEDTLSENCRYPTIONHANDLER_H

examples/mbed/stm32OledEncoder/generated/stm32OledEncoder_menu.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@
1212
#include "stm32OledEncoder_menu.h"
1313
#include "../ThemeMonoInverse.h"
1414

15+
16+
#include "MbedTlsEncryptionHandler.h"
17+
1518
// Global variable declarations
19+
20+
uint8_t keyData[] = { 0x13, 0x87, 0x6f, 0x1e, 0x3b, 0x66, 0xb6, 0xe2, 0xd5, 0xfc, 0x48, 0xf3, 0xea, 0xba, 0x5d, 0xc9, 0x57, 0x1a, 0x24, 0x3d, 0xbd, 0x3c, 0x8b, 0xf8, 0x61, 0x4f, 0x46, 0x9f, 0xb1, 0x6d, 0xa0, 0x51};
21+
22+
MbedTlsEncryptionHandler encryptionHandler(keyData);
23+
1624
const ConnectorLocalInfo applicationInfo = { "Demo mbed", "f5325e26-a7f6-40ff-876e-47afa06df532" };
1725
TcMenuRemoteServer remoteServer(applicationInfo);
1826
HalStm32EepromAbstraction glBspRom;
@@ -21,7 +29,7 @@ Adafruit_SSD1306_Spi gfx(spi, PD_15, PF_12, PF_13, 64, 128, SSD_1306);
2129
AdafruitDrawable gfxDrawable(&gfx);
2230
GraphicsDeviceRenderer renderer(30, applicationInfo.name, &gfxDrawable);
2331
MbedEthernetInitialiser mbedEthInitialisation(3333);
24-
MBedEthernetTransport ethernetTransport;
32+
MBedEthernetTransport ethernetTransport(&encryptionHandler);
2533
TagValueRemoteServerConnection ethernetConnection(ethernetTransport, mbedEthInitialisation);
2634

2735
// Global Menu Item declarations
@@ -99,6 +107,21 @@ void setupMenu() {
99107
renderer.setUseSliderForAnalog(false);
100108
installMonoInverseTitleTheme(renderer, MenuFontDef(nullptr, 1), MenuFontDef(nullptr, 1), true);
101109

110+
encryptionHandler.initialise();
111+
112+
char data[64] = {0};
113+
uint8_t encrypted[64] = {0};
114+
115+
strcpy(data, "hello world this is text.");
116+
serlogF2(SER_DEBUG, "Source: ", data);
117+
118+
auto dataLen = encryptionHandler.encryptData((uint8_t*)data, strlen(data), encrypted, sizeof encrypted);
119+
serlogHexDump(SER_DEBUG, "encrypted: ", encrypted , 60);
120+
121+
encryptionHandler.decryptData(encrypted, dataLen, (uint8_t*)data, sizeof data);
122+
serlogHexDump(SER_DEBUG, "plaintext: ", data, 60);
123+
serlogF2(SER_DEBUG, "pt out: ", data);
124+
102125
// We have an IoT monitor, register the server
103126
menuIoTMonitor.setRemoteServer(remoteServer);
104127

src/remote/BaseBufferedRemoteTransport.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ namespace tcremote {
99

1010
BaseBufferedRemoteTransport::BaseBufferedRemoteTransport(BufferingMode bufferMode, uint8_t readBufferSize,
1111
uint8_t writeBufferSize, EncryptionHandler* encHandler)
12-
: TagValueTransport(TVAL_BUFFERED), writeBufferSize(writeBufferSize),
13-
readBufferSize(readBufferSize), writeBufferPos(0), readBufferPos(0), encryptionBufferPos(0), readBufferAvail(0),
12+
: TagValueTransport(TVAL_BUFFERED), writeBufferSize(writeBufferSize), readBufferSize(readBufferSize),
13+
encryptionBuffer(nullptr), writeBufferPos(0), readBufferPos(0), encryptionBufferPos(0), readBufferAvail(0),
1414
encryptionHandler(encHandler), mode(bufferMode),
1515
ticksSinceWrite(0) {
1616
if(mode != BUFFER_ONE_MESSAGE && encHandler != nullptr) {
@@ -19,7 +19,9 @@ namespace tcremote {
1919
}
2020
readBuffer = new uint8_t[readBufferSize];
2121
writeBuffer = new uint8_t[writeBufferSize];
22-
encryptionBuffer = new uint8_t[readBufferSize];
22+
if(encHandler != nullptr) {
23+
encryptionBuffer = new uint8_t[readBufferSize];
24+
}
2325
}
2426

2527
BaseBufferedRemoteTransport::~BaseBufferedRemoteTransport() {

src/remote/BaseBufferedRemoteTransport.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ namespace tcremote {
2727
class EncryptionHandler {
2828
public:
2929
/**
30-
* Encrypt plain text data into encrypted format into the buffer
30+
* Encrypt plain text data into encrypted format into the buffer. Do note that plainText must be a mutable
31+
* buffer and must be a multiple of 16 bytes
3132
* @param plainText the plain bytes to encrypt
3233
* @param bytesIn the number of bytes to encrypt
3334
* @param buffer the output encrypted message
3435
* @param buffLen the buffer maximum length
3536
* @return the number of bytes encrypted or 0 if it fails.
3637
*/
37-
virtual int encryptData(const uint8_t *plainText, int bytesIn, const uint8_t *buffer, size_t buffLen) = 0;
38+
virtual int encryptData(uint8_t *plainText, int bytesIn, uint8_t *buffer, size_t buffLen) = 0;
3839
/**
3940
* Decrypt data from the wire into plain text and store the output into the buffer
4041
* @param encoded the encoded data to decrypt
@@ -43,7 +44,7 @@ namespace tcremote {
4344
* @param buffLen the size of the buffer
4445
* @return the number of bytes returned, or 0 if it fails.
4546
*/
46-
virtual int decryptData(const uint8_t *encoded, int bytesIn, const uint8_t *buffer, size_t buffLen) = 0;
47+
virtual int decryptData(const uint8_t *encoded, int bytesIn, uint8_t *buffer, size_t buffLen) = 0;
4748
};
4849

4950

0 commit comments

Comments
 (0)