Commit 1919794d authored by yids's avatar yids
Browse files

sending and receiving public keys works now.

parent 3b56c073
Pipeline #6 skipped
BOARD_TAG=atmega328
ARDUINO_LIBS=RadioHead SPI AESLib
ARDUINO_LIBS=RadioHead SPI AESLib Wire LCDI2C_LK162
include /usr/share/arduino/Arduino.mk
DEVICE_PATH = /dev/ttyUSB1
DEVICE_PATH = /dev/ttyUSB2
TARGET = arduino-rx
......@@ -9,15 +9,25 @@
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
#include <RHDatagram.h>
#include <SPI.h> // Not actualy used but needed to compile
#include <string.h>
#include <Wire.h>
#include <LCDI2C_LK162-12.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
LCDI2C lcd = LCDI2C(2,16,0x50,0);
aes_context ctx; // context for the cbc crypto stuff
RH_ASK driver;
#define ADDRESS 2
RH_ASK driver(5000);
RHDatagram manager(driver, ADDRESS);
// pubkey in readable format
uint8_t pubkey[2][NUM_ECC_DIGITS];
//my pub-priv keypair
uint8_t privkey[NUM_ECC_DIGITS] = {0x4D, 0xDE, 0x17, 0xCE, 0xAC, 0x4F, 0x81, 0x25, 0xA9, 0xE5, 0xC1, 0x82, 0x79, 0x65, 0x71, 0x13, 0x47, 0xD6, 0xF4, 0x3B, 0x5E, 0x1F, 0x27, 0x53};
......@@ -25,11 +35,7 @@ EccPoint localPubkey = {
{0xF3, 0xCD, 0x7D, 0x81, 0x15, 0x01, 0xC0, 0x3E, 0xB5, 0x56, 0x5B, 0xF1, 0x1A, 0x99, 0xFB, 0x0B, 0x20, 0x2B, 0x18, 0x82, 0xD3, 0x71, 0x6F, 0x1E},
{0x4B, 0x84, 0xC7, 0xDD, 0x67, 0x6B, 0xED, 0x45, 0x1A, 0x4B, 0x54, 0x5A, 0x19, 0xCA, 0x1B, 0x6F, 0x3B, 0x93, 0xBA, 0xF4, 0x2A, 0x9B, 0x65, 0x69}};
// other pub key
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},
{0xE1, 0xB1, 0x28, 0x7F, 0xD3, 0xB0, 0x94, 0x24, 0x69, 0x95, 0xDE, 0x76, 0x40, 0x15, 0x6E, 0xAE, 0x74, 0xE9, 0x56, 0x94, 0xA7, 0xE4, 0x63, 0xEF}};
EccPoint remotePubkey;
void p(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars
......@@ -86,24 +92,70 @@ char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
// returns the shared secret
uint8_t calcSharedSecret(EccPoint *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS])
{
Serial.println("Calculating shared secret...");
Serial.print("Calculating shared secret...");
uint8_t secret[NUM_ECC_DIGITS];
ecdh_shared_secret(secret, p_pubkey, p_privkey, NULL);
// dump("secret", secret);
//dump("secret", secret);
return *secret;
}
void setup()
{
lcd.init();
lcd.println("dit is een pager");
Serial.begin(9600); // Debugging only
Serial.println("hoi");
if (!driver.init())
if (!manager.init())
Serial.println("init failed");
randomSeed(analogRead(0));
}
void loop()
{
// crypto vars //
uint8_t sharedSecret;
// Radio vars //
uint8_t* receivedData;
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
uint8_t from;
uint8_t to;
uint8_t id;
manager.waitAvailable();
if (manager.recvfrom(buf, &buflen, &from, &to ,&id))
{
Serial.print("got message from : ");
Serial.print(from, DEC);
Serial.print(" : ");
Serial.print(id);
Serial.println(" : ");
if (id == 50){ // receiving a public key...
for (int i = 0 ; i < NUM_ECC_DIGITS ; i++){
remotePubkey.x[i] = buf[i];
//Serial.println(remotePubkey.x[i]);
}
for (int i = NUM_ECC_DIGITS ; i < NUM_ECC_DIGITS*2 ; i++){
remotePubkey.y[i-NUM_ECC_DIGITS] = buf[i];
//Serial.println(remotePubkey.y[i-NUM_ECC_DIGITS]);
}
}
if (id == 51){ // receiving an encrypted message...
Serial.println("Storing received message");
receivedData = buf;
}
}
if( ecc_valid_public_key(&remotePubkey) == 1 ){
Serial.println("Valid public key received");
sharedSecret = calcSharedSecret(&remotePubkey, privkey);
Serial.println("Done!");
}
Serial.println((char*)receivedData);
/*
// Crypto //
char* data = "Hallo dit is een testbericht jwz";
......@@ -122,17 +174,6 @@ void loop()
Serial.println(decryptedData);
Serial.println ("");
// Radio //
/*
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (driver.recv(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump it.
driver.printBuffer("Got encrypted:", buf, buflen);
}
*/
}
......@@ -3,4 +3,4 @@ ARDUINO_LIBS=RadioHead SPI AESLib
include /usr/share/arduino/Arduino.mk
DEVICE_PATH = /dev/ttyUSB1
TARGET = arduino-tx
TARGET = arduino-tx
......@@ -9,18 +9,26 @@
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
#include <RHDatagram.h>
#include <SPI.h> // Not actually used but needed to compile
RH_ASK driver;
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
#define ADDRESS 5
aes_context ctx;
RH_ASK driver(5000);
RHDatagram manager(driver, ADDRESS);
//my pub-priv keypair
uint8_t private_0[NUM_ECC_DIGITS] = {0x14, 0x14, 0x92, 0x2D, 0x2E, 0x00, 0x87, 0x16, 0x2D, 0x43, 0x0E, 0xC1, 0x8A, 0xD0, 0x0A, 0x1E, 0xEE, 0x89, 0x4F, 0x17, 0x3F, 0xB1, 0x1B, 0x5A};
EccPoint public_0 = {
uint8_t privkey[NUM_ECC_DIGITS] = {0x14, 0x14, 0x92, 0x2D, 0x2E, 0x00, 0x87, 0x16, 0x2D, 0x43, 0x0E, 0xC1, 0x8A, 0xD0, 0x0A, 0x1E, 0xEE, 0x89, 0x4F, 0x17, 0x3F, 0xB1, 0x1B, 0x5A};
EccPoint localPubkey = {
{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}};
//other pubkey
EccPoint public_1 = {
EccPoint remotePubkey = {
{0xF3, 0xCD, 0x7D, 0x81, 0x15, 0x01, 0xC0, 0x3E, 0xB5, 0x56, 0x5B, 0xF1, 0x1A, 0x99, 0xFB, 0x0B, 0x20, 0x2B, 0x18, 0x82, 0xD3, 0x71, 0x6F, 0x1E},
{0x4B, 0x84, 0xC7, 0xDD, 0x67, 0x6B, 0xED, 0x45, 0x1A, 0x4B, 0x54, 0x5A, 0x19, 0xCA, 0x1B, 0x6F, 0x3B, 0x93, 0xBA, 0xF4, 0x2A, 0x9B, 0x65, 0x69}};
......@@ -41,32 +49,100 @@ void dump(char *text, uint8_t *d) {
Serial.print("\n");
}
void shareSecret()
uint8_t* generateIV()
{
uint8_t buffer[BLOCK_SIZE];
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)
{
uint8_t secret_0[NUM_ECC_DIGITS] = { 0, };
ecdh_shared_secret(secret_0, &public_1, private_0, NULL);
dump("secret", secret_0);
Serial.print("data:");
Serial.println(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");
return p_data;
}
char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
{
Serial.print("decrypting data...");
aes128_cbc_dec_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_dec_finish(ctx);
Serial.println("done");
return p_data;
}
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t calcSharedSecret(EccPoint *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS])
{
Serial.println("Calculating shared secret...");
uint8_t secret[NUM_ECC_DIGITS];
ecdh_shared_secret(secret, p_pubkey, p_privkey, NULL);
//dump("secret", secret);
return *secret;
}
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("het werkt");
if (!driver.init())
if (!manager.init())
Serial.println("init failed");
}
void loop()
void sendPubkey()
{
shareSecret();
char *msg = "hello";
uint8_t key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
//char data[] = "0123456789012345"; //16 chars == 16 bytes
char* msg = "pubkey";
uint8_t pubkey[NUM_ECC_DIGITS*2];
aes128_enc_single(key, msg);
for(uint8_t i = 0; i < NUM_ECC_DIGITS; i++){
pubkey[i] = localPubkey.x[i];
}
for(uint8_t i = NUM_ECC_DIGITS; i < NUM_ECC_DIGITS*2; i++){
pubkey[i] = localPubkey.y[i-NUM_ECC_DIGITS];
}
manager.setHeaderId(50);
manager.sendto((uint8_t *)pubkey, 48, 2);
manager.waitPacketSent();
Serial.println("sending public key...");
}
void sendMessage()
{
Serial.println("Sending message...");
char* msg = "hallo";
manager.setHeaderId(51);
manager.sendto((uint8_t *)msg, 48, 2);
manager.waitPacketSent();
}
void loop()
{
// crypto vars //
uint8_t sharedSecret;
char* data = "Hallo";
char* encryptedData;
uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
driver.send((uint8_t *)msg, strlen(msg));
driver.waitPacketSent();
delay(200);
sendPubkey();
sharedSecret = calcSharedSecret(&remotePubkey, privkey);
ctx = aes128_cbc_enc_start(&sharedSecret, iv);
// encryptedData = encryptAES(&sharedSecret, data, ctx);
delay(10000);
sendMessage();
delay(5000);
}
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