firmware update to support display on ESP32-C6-LCD-1.47

This commit is contained in:
schauausgmbh committed 2026-09-30 15:55:27 +02:00
1 parent a66ccd75c0
commit 13af4e9ff3
4 files changed
+409 -73

No files matched your search

+171 -67
View File
@@ -20,38 +20,72 @@
* DEALINGS IN THE SOFTWARE.
*/
// compiler values definition
#define PRINT_SERIAL 1
#define DEBUG 0
#define USE_DISPLAY 1
#define HAS_KEYBOARD 0
#define BTN_HOME 9
#define MAX_INPUT_LENGHT 256
// external libraries
#include <Crypto.h>
#include <AES.h>
#include <string.h>
#define MAX_INPUT_LENGHT 256
// project files to include
#include "display.h"
//creating an object of AES256 class
AES256_ESP aes256;
void printHelp () {
Serial.println();
Serial.println("┌-----------------------------------------------------┐");
Serial.println("| Encrypt / Decrypt seed phrase with AES256 algorithm |");
Serial.println("└-----------------------------------------------------┘");
Serial.println();
Serial.println("WARNING: DO NOT USE THIS ON ANY NETWORK CONNECTED DEVICE AND ENSURE THE DECRYPTION IS WORKING PROPERLY!");
Serial.println("USE IN SECURE ENVIRONMENTS ONLY!");
Serial.println("This program is offered as it is and no warranties will be granted in any case!");
Serial.println("Review the source code thoroughly before using it for real application!");
Serial.println("Available commands:");
Serial.println("help - print this help to serial console");
Serial.println("key - generate random HEX key and print it to the serial console");
Serial.println("key <your 32byte key> - set key (hexadecimal or ascii) for encryption / decryption");
Serial.println("encrypt <12 or 24 word seed phrase> - encrypts your 12 ord 24 word plain text seed phrase to HEX string");
Serial.println("decrypt <encrypted HEX string> - decrypts your encrypted HEX string back to clear text seed phrase");
Serial.println("clear - clear all cryptographic information from memory");
Serial.println("restart - reboot the device for a fresh restart; any working data will be removed from memory");
#if PRINT_SERIAL
Serial.println();
Serial.println("┌-----------------------------------------------------┐");
Serial.println("| Encrypt / Decrypt seed phrase with AES256 algorithm |");
Serial.println("└-----------------------------------------------------┘");
Serial.println();
Serial.println("WARNING: DO NOT USE THIS ON ANY NETWORK CONNECTED DEVICE AND ENSURE THE DECRYPTION IS WORKING PROPERLY!");
Serial.println("USE IN SECURE ENVIRONMENTS ONLY!");
Serial.println("This program is offered as it is and no warranties will be granted in any case!");
Serial.println("Review the source code thoroughly before using it for real application!");
Serial.println("Available commands:");
Serial.println("help - print this help to serial console");
Serial.println("key - generate random HEX key and print it to the serial console");
Serial.println("key <your 32byte key> - set key (hexadecimal or ascii) for encryption / decryption");
Serial.println("encrypt <12 or 24 word seed phrase> - encrypts your 12 ord 24 word plain text seed phrase to HEX string");
Serial.println("decrypt <encrypted HEX string> - decrypts your encrypted HEX string back to clear text seed phrase");
Serial.println("clear - clear all cryptographic information from memory");
Serial.println("restart - reboot the device for a fresh restart; any working data will be removed from memory");
#endif
#if USE_DISPLAY
display_help();
#endif
}
void byteToHex (byte b, char* returnVal) {
static const char hexChars[] = "0123456789ABCDEF";
//char returnVal[4];
returnVal[0] = hexChars[b >> 4];
returnVal[1] = hexChars[b & 0x0F];
returnVal[2] = ' ';
returnVal[3] = '\0';
#if PRINT_SERIAL
Serial.print(returnVal);
#endif
}
void setCryptKey (String argKey) {
Serial.println();
Serial.println("Operation: setting encryption key");
#if PRINT_SERIAL
Serial.println();
Serial.println("Operation: setting encryption key");
#endif
argKey.trim();
String tempKey = argKey;
byte cryptKey[32];
@@ -59,19 +93,18 @@ void setCryptKey (String argKey) {
tempKey.replace(" ", ""); // remove any spaces in between characters to check for hexadecimal values
if(argKey.length() != 32 && tempKey.length() / 2 != 32) { // there was no valid key given; keys need to consist of 32 characters (256bit)
Serial.println("No valid key handed over; generating new key:");
#if PRINT_SERIAL
Serial.println("No valid key handed over; generating new key:");
#endif
for(int i=0; i < 32; i++) {
cryptKey[i] = random(256);
}
for(size_t i=0; i<32; i++) {
if(cryptKey[i] < 16) Serial.print("0");
Serial.print(cryptKey[i], HEX);
Serial.print(" ");
}
Serial.println();
}
else { // process valid key provided
Serial.println("Valid key detected");
#if PRINT_SERIAL
Serial.println("Valid key detected");
#endif
bool keyIsHex = true;
for(size_t i = 0; i < tempKey.length(); i++)
@@ -96,17 +129,33 @@ void setCryptKey (String argKey) {
}
// set the final key for encryption / decryption
aes256.setKey(cryptKey, 32);
Serial.println("Key set for encryption / decryption");
#if PRINT_SERIAL
Serial.println("Key set for encryption / decryption");
for(size_t i=0; i<32; i++) {
if(cryptKey[i] < 16) Serial.print("0");
Serial.print(cryptKey[i], HEX);
Serial.print(" ");
}
Serial.println();
#endif
#if USE_DISPLAY
display_key(cryptKey);
#endif
}
void decryptData (String data) {
Serial.println();
Serial.println("Operation: decrypting data");
#if PRINT_SERIAL
Serial.println();
Serial.println("Operation: decrypting data");
#endif
data.replace(" ", "");
byte dataBlock[aes256.blockSize()];
byte plainText[aes256.blockSize()];
String byteString = "";
String fullString = "";
// check whether the input data is a hexadecimal string
bool dataIsHex = true;
@@ -117,11 +166,16 @@ void decryptData (String data) {
dataIsHex = false;
if(!dataIsHex) {
Serial.println("Abort decryption! No valid input data!");
#if PRINT_SERIAL
Serial.println("Abort decryption! No valid input data!");
#endif
return;
}
Serial.println("decrypted data block: ");
#if PRINT_SERIAL
Serial.println("decrypted data block: ");
#endif
for(int i=0; i < data.length(); i += aes256.blockSize() * 2) {
for(int j=0; j<aes256.blockSize() * 2; j+=2) {
byteString = data.substring(i + j, i + j + 2);
@@ -131,20 +185,35 @@ void decryptData (String data) {
aes256.decryptBlock(plainText, dataBlock);
for(int i=0; i<sizeof(plainText); i++){
if(plainText[i] == '\0') break;
Serial.write(plainText[i]);
#if PRINT_SERIAL
Serial.write(plainText[i]);
#endif
fullString += (char)plainText[i];
}
}
Serial.println();
#if PRINT_SERIAL
Serial.println();
#endif
#if USE_DISPLAY
display_decrypt_text(fullString, BTN_HOME);
#endif
}
void encryptData (String data) {
Serial.println();
Serial.println("Operation: encrypting data");
#if PRINT_SERIAL
Serial.println();
Serial.println("Operation: encrypting data");
#endif
byte dataBlock[aes256.blockSize()];
byte cypher[aes256.blockSize()];
String fullString = "";
char hexStr[4];
#if PRINT_SERIAL
Serial.println("encrypted data block: ");
#endif
Serial.println("encrypted data block: ");
for(int i=0; i<data.length(); i+=aes256.blockSize()) {
for(int j=0; j<aes256.blockSize(); j++) {
dataBlock[j] = data[i + j];
@@ -152,57 +221,92 @@ void encryptData (String data) {
}
aes256.encryptBlock(cypher, dataBlock);
for(int j=0; j<sizeof(cypher); j++) {
if(cypher[j] < 16) Serial.print("0");
Serial.print(cypher[j], HEX);
Serial.print(" ");
byteToHex(cypher[j], hexStr);
fullString += hexStr;
}
}
Serial.println();
#if PRINT_SERIAL
Serial.println();
#endif
display_crypt_block(fullString, BTN_HOME);
}
void clearCryptograficInformation () {
Serial.println();
Serial.println("Operation: clear all cryptographic information from memory");
#if PRINT_SERIAL
Serial.println();
Serial.println("Operation: clear all cryptographic information from memory");
#endif
aes256.clear();
Serial.println("Memory cleared");
#if PRINT_SERIAL
Serial.println("Memory cleared");
#endif
display_home();
}
void restartDevice () {
Serial.println("\r\nInitiating reboot of this device!\r\n");
#if PRINT_SERIAL
Serial.println("\r\nInitiating reboot of this device!\r\n");
#endif
ESP.restart();
}
void readCmd () {
Serial.println();
Serial.println("Waiting for command (send \"help\" for available commands)");
while (Serial.available() == 0) {} // wait until input on the serial console is available
String inp = Serial.readString();
inp.trim();
String cmd = "";
String arg = "";
if ( Serial.available() ) {
String inp = Serial.readString();
inp.trim();
String cmd = "";
String arg = "";
if(inp.indexOf(' ') > 0) {
cmd = inp.substring(0, inp.indexOf(' '));
arg = inp.substring(inp.indexOf(' ') + 1);
arg.trim();
if(inp.indexOf(' ') > 0) {
cmd = inp.substring(0, inp.indexOf(' '));
arg = inp.substring(inp.indexOf(' ') + 1);
arg.trim();
}
else
cmd = inp;
if (cmd == "help") printHelp();
else if(cmd == "key") setCryptKey(arg);
else if (cmd == "encrypt") encryptData(arg);
else if (cmd == "decrypt") decryptData(arg);
else if (cmd == "clear") clearCryptograficInformation();
else if (cmd == "restart") restartDevice();
else {
#if PRINT_SERIAL
Serial.println("\r\nUnknown command! Try again!");
#endif
}
}
else
cmd = inp;
}
if (cmd == "help") printHelp();
else if(cmd == "key") setCryptKey(arg);
else if (cmd == "encrypt") encryptData(arg);
else if (cmd == "decrypt") decryptData(arg);
else if (cmd == "clear") clearCryptograficInformation();
else if (cmd == "restart") restartDevice();
else Serial.println("\r\nUnknown command! Try again!");
void readHome() {
if(!digitalRead(BTN_HOME)) { // Home-Button pressed (pulled high with 10K resistor on the board)
#if USE_DISPLAY
display_home();
#endif
#if PRINT_SERIAL
Serial.println("Waiting for command (send \"help\" for available commands)");
#endif
delay(1000);
}
}
void setup() {
Serial.begin(115200);
printHelp();
while(!Serial);
pinMode(BTN_HOME, INPUT);
display_setup();
Serial.println("Waiting for command");
#if PRINT_SERIAL
Serial.println("send \"help\" for available commands");
#endif
}
void loop() {
readCmd();
readHome(); // check for Home-button being pressed
}