Commit 3b56c073 authored by yids's avatar yids
Browse files

now uses cbc to sent bigger messages

parent 5331afd0
Pipeline #4 skipped
...@@ -10,6 +10,12 @@ ...@@ -10,6 +10,12 @@
#include <RadioHead.h> #include <RadioHead.h>
#include <RH_ASK.h> #include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile #include <SPI.h> // Not actualy used but needed to compile
#include <string.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
aes_context ctx; // context for the cbc crypto stuff
RH_ASK driver; RH_ASK driver;
...@@ -24,6 +30,7 @@ EccPoint remotePubkey = { ...@@ -24,6 +30,7 @@ EccPoint remotePubkey = {
{0x90, 0x6F, 0xFC, 0xC8, 0x07, 0xA8, 0x93, 0x9C, 0x3B, 0xDC, 0xAF, 0xF1, 0x8D, 0xA9, 0x96, 0xF8, 0xC2, 0x3B, 0x85, 0x3E, 0x5C, 0x65, 0xDE, 0x32}, {0x90, 0x6F, 0xFC, 0xC8, 0x07, 0xA8, 0x93, 0x9C, 0x3B, 0xDC, 0xAF, 0xF1, 0x8D, 0xA9, 0x96, 0xF8, 0xC2, 0x3B, 0x85, 0x3E, 0x5C, 0x65, 0xDE, 0x32},
{0xE1, 0xB1, 0x28, 0x7F, 0xD3, 0xB0, 0x94, 0x24, 0x69, 0x95, 0xDE, 0x76, 0x40, 0x15, 0x6E, 0xAE, 0x74, 0xE9, 0x56, 0x94, 0xA7, 0xE4, 0x63, 0xEF}}; {0xE1, 0xB1, 0x28, 0x7F, 0xD3, 0xB0, 0x94, 0x24, 0x69, 0x95, 0xDE, 0x76, 0x40, 0x15, 0x6E, 0xAE, 0x74, 0xE9, 0x56, 0x94, 0xA7, 0xE4, 0x63, 0xEF}};
void p(char *fmt, ... ){ void p(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars char tmp[128]; // resulting string limited to 128 chars
va_list args; va_list args;
...@@ -41,21 +48,36 @@ void dump(char *text, uint8_t *d) { ...@@ -41,21 +48,36 @@ void dump(char *text, uint8_t *d) {
Serial.print("\n"); Serial.print("\n");
} }
char* encryptAES(uint8_t* p_secret, char* p_data) uint8_t* generateIV()
{ {
Serial.print("\n"); uint8_t buffer[BLOCK_SIZE];
Serial.println("encoding data..."); Serial.println("generating IV...");
for (int i=0 ; i<BLOCK_SIZE ; i++){
buffer[i] = random(64);
}
for (int i = 0; i < 17; i++){
Serial.print(buffer[i]);
}
Serial.println("");
return buffer;
}
char* encryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
{
Serial.print("data:");
Serial.println(p_data); Serial.println(p_data);
aes128_enc_single(p_secret, p_data); Serial.print("encrypting data...");
aes128_cbc_enc_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_enc_finish(ctx);
Serial.println("done"); Serial.println("done");
return p_data; return p_data;
} }
char* decryptAES(uint8_t* p_secret, char* p_data) char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
{ {
Serial.print("\n"); Serial.print("decrypting data...");
Serial.println("decoding data..."); aes128_cbc_dec_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_dec_single(p_secret, p_data); aes128_cbc_dec_finish(ctx);
Serial.println("done"); Serial.println("done");
return p_data; return p_data;
} }
...@@ -77,26 +99,31 @@ void setup() ...@@ -77,26 +99,31 @@ void setup()
Serial.println("hoi"); Serial.println("hoi");
if (!driver.init()) if (!driver.init())
Serial.println("init failed"); Serial.println("init failed");
randomSeed(analogRead(0));
} }
void loop() void loop()
{ {
// Crypto // // Crypto //
uint8_t sharedSecret = calcSharedSecret(&remotePubkey, privkey); char* data = "Hallo dit is een testbericht jwz";
char data[] = "0123456789012345"; uint8_t sharedSecret = calcSharedSecret(&remotePubkey, privkey);
char* encodedData; char* encryptedData;
char* decodedData; char* decryptedData;
encodedData = encryptAES(&sharedSecret, data); // uint8_t* iv = generateIV();
Serial.print("encoded data:"); uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
Serial.println(encodedData); ctx = aes128_cbc_enc_start(&sharedSecret, iv);
encryptedData = encryptAES(&sharedSecret, data, ctx);
decodedData = decryptAES(&sharedSecret, encodedData); ctx = aes128_cbc_enc_start(&sharedSecret, iv);
Serial.print("decoded data:"); decryptedData = decryptAES(&sharedSecret, encryptedData, ctx);
Serial.println(decodedData); Serial.print("decrypted data:");
Serial.println(decryptedData);
Serial.println ("");
// Radio // // Radio //
/*
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN]; uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf); uint8_t buflen = sizeof(buf);
...@@ -107,4 +134,5 @@ void loop() ...@@ -107,4 +134,5 @@ void loop()
// Message with a good checksum received, dump it. // Message with a good checksum received, dump it.
driver.printBuffer("Got encrypted:", buf, buflen); driver.printBuffer("Got encrypted:", buf, buflen);
} }
*/
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment