Yes, I can share the code. It is not finished yet, so that can make it a bit confusing.
I had a code where the Arduino sensor data is encrypted with AES encryption and after that send to the raspberry PI.
Now, I am working on the code to change it in such a way that the Arduino receives the AES Key and IV from the raspberry PI. So the Arduino does not generate the AES Key and IV by itself.
In the loop, the KEY and IV received from the raspberry PI will be used to send encrypted sensor data.
#include "AESLib.h"
#define BAUD 9600
char ID[] = "HY08V21234567865"; //ID of container
String key;
String IV;
AESLib aesLib;
#define INPUT_BUFFER_LIMIT (128+1) // designed for Arduino UNO, not stress-tested anymore (this works with readBuffer[129])
#define trigPin 8
#define echoPin 10
long duration;
float longitude;
float latitude;
int k; //determine length long
unsigned char cleartext[INPUT_BUFFER_LIMIT] = {0}; // THIS IS INPUT BUFFER (FOR TEXT)
unsigned char ciphertext[2*INPUT_BUFFER_LIMIT] = {0}; // THIS IS OUTPUT BUFFER (FOR BASE64-ENCODED ENCRYPTED DATA)
unsigned char readBuffer [18] = {0}; //Waarde 0 er achter gezet omdat het nog toegekend moet worden.
// AES Encryption Key (same as in node-js example)
byte aes_key[] = { 57, 36, 24, 25, 28, 86, 32, 41, 31, 36, 91, 36, 51, 74, 63, 89 }; //DELETE LATER ON
// General initialization vector (same as in node-js example) (you must use your own IV's in production for full security!!!)
byte aes_iv[16] = { 52, 58, 87, 63}; //DELETE LATER ON
// Generate IV (once) DELETE LATER ON
void aes_init() {
aesLib.gen_iv(aes_iv);
aesLib.set_paddingmode((paddingMode)0);
}
uint16_t encrypt_to_ciphertext(char * msg, uint16_t msgLen, byte iv[]) {
//Serial.println("Calling encrypt (string)...");
// aesLib.get_cipher64_length(msgLen);
int cipherlength = aesLib.encrypt((byte*)msg, msgLen, (char*)ciphertext, aes_key, sizeof(aes_key), iv);
// uint16_t encrypt(byte input[], uint16_t input_length, char * output, byte key[],int bits, byte my_iv[]);
return cipherlength;
}
uint16_t decrypt_to_cleartext(byte msg[], uint16_t msgLen, byte iv[]) {
Serial.print("Calling decrypt...; ");
uint16_t dec_bytes = aesLib.decrypt(msg, msgLen, (char*)cleartext, aes_key, sizeof(aes_key), iv);
Serial.print("Decrypted bytes: "); Serial.println(dec_bytes);
return dec_bytes;
}
void setup() {
// Define inputs and outputs:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(BAUD);
Serial.setTimeout(15000); //was 60000 in de originele code
Serial.print("Hello World, from Arduino");
Serial.print(";");
Serial.println(ID);
delay(10);
while (!Serial.available());
key = Serial.readString();
Serial.println(key);
while (!Serial.available());
IV = Serial.readString();
Serial.println(IV);
aes_init(); // generate random IV, should be called only once? causes crash if repeated...
}
/* non-blocking wait function */
void wait(unsigned long milliseconds) {
unsigned long timeout = millis() + milliseconds;
while (millis() < timeout) {
yield();
}
}
unsigned long loopcount = 0;
// Working IV buffer: Will be updated after encryption to follow up on next block.
// But we don't want/need that in this test, so we'll copy this over with enc_iv_to/enc_iv_from
// in each loop to keep the test at IV iteration 1. We could go further, but we'll get back to that later when needed.
// General initialization vector (same as in node-js example) (you must use your own IV's in production for full security!!!)
byte enc_iv[16] = { 52 };
byte enc_iv_to[16] = { 52 };
byte enc_iv_from[16] = { 52 };