Commit b69e4fdb authored by hark's avatar hark

blabla

parent e263b258
......@@ -7,3 +7,7 @@ include /usr/share/arduino/Arduino.mk
#DEVICE_PATH = /dev/ttyUSB0
DEVICE_PATH = /tmp/simavr-uart0
TARGET = arduino-rx
debugger: all
ddd --debugger /home/user/.platformio/packages/toolchain-atmelavr/bin/avr-gdb -x gdb.conf
......@@ -14,20 +14,11 @@
#include <LiquidCrystal.h>
#include <avr/pgmspace.h>
/*
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
*/
//LCDI2C lcd = LCDI2C(2,16,0x50,0);
/*
* pagerlib things
*/
aes_context ctx; // context for the cbc crypto stuff
//const uint8_t tx_priv_key[ECC_COMPRESSED_SIZE-1] PROGMEM = {0xB4, 0x15, 0x70, 0x3E, 0xA7, 0x5D, 0x06, 0xF2, 0x33, 0x75, 0x8E, 0xE0, 0x86, 0x1B, 0x73, 0xBE, 0xEC, 0x87, 0x36, 0x0F, 0xF5, 0xE1, 0x79, 0x76, 0x2C, 0x3C, 0x74, 0x69, 0x83, 0x71, 0x07, 0xB5};
//const uint8_t tx_comp_p[ECC_COMPRESSED_SIZE] PROGMEM = {0x02, 0xA3, 0x91, 0x16, 0x71, 0x1E, 0x7B, 0xB2, 0x51, 0x7F, 0xD4, 0xF4, 0xC7, 0x81, 0x49, 0x75, 0x8B, 0x48, 0x75, 0x59, 0x27, 0xC9, 0x94, 0x3F, 0x59, 0xDD, 0xFF, 0x2E, 0x89, 0xE9, 0xD8, 0x1D, 0x2B };
struct pl_keypair *sender, *receiver, *kp;
char clear_message[] = "dit is een test berichtje :) ";
//const char clear_message[] PROGMEM = "Blaat coblaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
......@@ -114,17 +105,30 @@ void setup()
* load the keys from the base64 thingies
*/
receiver = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
base64_decode((char *)receiver , (char *)&b64_key, (4*sizeof(struct pl_keypair) / 3)) ;
receiver = pl_create_keypair(context);
if (receiver == NULL)
{
Serial.println("creating receiver keypair failed! err:");
Serial.println(context->err);
}
if (1) {
Serial.println("1 is waar");
}
//receiver = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
//base64_decode((char *)receiver , (char *)&b64_key, (4*sizeof(struct pl_keypair) / 3)) ;
/*
pl_load_key_in_list(context, receiver);
// receiver = pl_create_keypair(context);
//
// create sender keypair
// kp = pl_create_keypair(context);
sender = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
context->kp = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
base64_decode((char *)sender , (char *)&b64_key2, (4*sizeof(struct pl_keypair) / 3)) ;
sender = pl_create_keypair(context);
if (sender == NULL)
{
Serial.println("creating sender keypair failed! err:");
Serial.println(context->err);
}
*/
//sender = (struct pl_keypair *)malloc(sizeof(struct pl_keypair));
//base64_decode((char *)sender , (char *)&b64_key2, (4*sizeof(struct pl_keypair) / 3)) ;
// pl_load_key_in_list(context, kp);
// set default
......@@ -239,10 +243,30 @@ void loop()
//get msg with radio
//memcpy(context->msg->msg + 8, "bla", MSG_SIZE - 8);
pl_send_message(context);
if( pl_send_message(context) != 0)
{
Serial.println("error in pl_send_message: ");
Serial.println(context->err);
context->err = 0;
} else {
Serial.println("no error");
}
Serial.println("crypted message addr:");
Serial.println(context->msg->address);
Serial.println("crypted message sender_dec_point:");
Serial.println((char *)context->decompressed_point);
Serial.println("crypted message sender_comp_point:");
Serial.println((char *)context->msg->sender_compressed_point);
Serial.println("crypted message iv:");
Serial.println((char *)&context->msg->iv);
Serial.println("crypted message msg:");
Serial.println((char *)context->msg->msg);
Serial.println("crypted message:");
Serial.println(context->msg->msg);
// base64_encode((char *)&output, context->msg->msg, MSG_SIZE);
#ifdef LCD
lcd.clear();
......@@ -254,7 +278,15 @@ void loop()
lcd.print(context->msg->msg);
#endif
// pl_receive_message(context);
/*
if (pl_receive_message(context) != 0)
{
Serial.println("error in pl_receiver_message");
Serial.println(context->err);
context->err = 0;
}
*/
#ifdef LCD
delay(1000);
lcd.clear();
......
......@@ -5,3 +5,7 @@ ARDUINO_LIBS=pagerlib RadioHead SPI AESLib micro-ecc MemoryFree
DEVICE_PATH = /dev/ttyUSB1
TARGET = arduino-tx
debugger: arduino-tx
ddd --debugger /home/user/.platformio/packages/toolchain-atmelavr/bin/avr-gdb -x gdb.conf
......@@ -49,7 +49,7 @@ int rng (uint8_t *dest, unsigned size) {
return 1;
#else
// arduino rng comes here
memcpy(dest, &fakeiv, size);
return 0;
#endif
}
......@@ -107,9 +107,9 @@ inline int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
*/
inline int pl_send_message(struct pl_ctx *ctx) {
memset(ctx->decompressed_point, 7, sizeof(ctx->decompressed_point));
memset(ctx->shared_secret, 7, sizeof(ctx->shared_secret));
// memset(ctx->msg->msg, 23, MSG_SIZE);
memset(ctx->msg->msg, 23, MSG_SIZE);
#ifndef ARDUINO
/* create a random iv */
......@@ -118,12 +118,12 @@ inline int pl_send_message(struct pl_ctx *ctx) {
memcpy(&ctx->msg->iv, &fakeiv, sizeof(ctx->msg->iv));
#endif
// copy compressed point into pager message
// copy compressed point from keypair into pager message
memcpy(&ctx->msg->sender_compressed_point, &ctx->kp->compressed_point, sizeof(ctx->msg->sender_compressed_point));
//DBG("Sending message \n");
/* decompress key */
uECC_decompress(ctx->receiver_compressed_point, ctx->decompressed_point, ctx->curve);
uECC_decompress(&ctx->receiver_compressed_point, &ctx->decompressed_point, ctx->curve);
// DBM("#receiver compressed point ",sizeof(ctx->receiver_compressed_point) , &ctx->receiver_compressed_point);
// DBM("#decompr point ",sizeof(ctx->decompressed_point) , &ctx->decompressed_point);
......@@ -133,8 +133,9 @@ inline int pl_send_message(struct pl_ctx *ctx) {
//printf(">>>>>>>>>>>>>>>>>>>address: %u \n \n", ctx->msg->address );
/*calculate shared secret on sender*/
if (!uECC_shared_secret((uint8_t *)&ctx->decompressed_point, (uint8_t *)&ctx->kp->private_key, (uint8_t *)&ctx->shared_secret, ctx->curve)) {
if (uECC_shared_secret((uint8_t *)&ctx->decompressed_point, (uint8_t *)&ctx->kp->private_key, (uint8_t *)&ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed in send_message (1)\n");
ctx->err = 10;
return 1;
}
......@@ -183,10 +184,10 @@ inline int pl_send_message(struct pl_ctx *ctx) {
return 0;
}
inline int pl_receive_message(struct pl_ctx * ctx)
int pl_receive_message(struct pl_ctx * ctx)
{
memset(&ctx->shared_secret, 7, sizeof(ctx->shared_secret));
memset(&ctx->clear_message, 7, sizeof(ctx->clear_message));
// memset(&ctx->shared_secret, 7, sizeof(ctx->shared_secret));
// memset(&ctx->clear_message, 7, sizeof(ctx->clear_message));
// DBM("msg->msg in pl_receive_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
......@@ -204,29 +205,30 @@ inline int pl_receive_message(struct pl_ctx * ctx)
}
}
// exit when address not found
if (found == 0) return 0;
if (found == 0) return 1;
#endif
/* decompress the senders public key */
uECC_decompress(ctx->msg->sender_compressed_point, ctx->decompressed_point, ctx->curve);
uECC_decompress(&ctx->msg->sender_compressed_point, &ctx->decompressed_point, ctx->curve);
//DBM("ctx->kp", sizeof(struct pl_pagermessage),&ctx->kp);
//DBM("ctx->kp->private_key", sizeof(ctx->kp->private_key),&ctx->kp->private_key);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(ctx->decompressed_point, ctx->kp->private_key, ctx->shared_secret, ctx->curve)) {
if (!uECC_shared_secret(&ctx->decompressed_point, &ctx->kp->private_key, &ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed (receive)\n");
ctx->err = 10;
}
// DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
#ifndef NOCRYPT
#ifdef ARDUINO
memcpy(ctx->clear_message, ctx->msg->msg, MSG_SIZE);
/*
// memcpy(ctx->clear_message, ctx->msg->msg, MSG_SIZE);
aes_context aes_ctx;
aes_ctx = aes192_cbc_dec_start(ctx->shared_secret, ctx->msg->iv);
aes192_cbc_dec_continue(aes_ctx, ctx->clear_message, MSG_SIZE);
aes192_cbc_dec_continue(aes_ctx, ctx->msg->msg, MSG_SIZE);
aes192_cbc_dec_finish(aes_ctx);
*/
#else
/* decrypt the message */
mbedtls_aes_init( &ctx->aes_ctx );
......@@ -246,20 +248,21 @@ inline int pl_receive_message(struct pl_ctx * ctx)
DBM("iv in pl_receive_message after crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
*/
// message decrypted, now store it in the inbox
#ifndef ARDUINO
pl_inbox_append(ctx, ctx->msg);
// make pointer to current message null to prevent overwriting the stored message
ctx->msg = NULL;
// make space for the next one
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
return 1;
#endif
return 0;
}
inline struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
struct pl_keypair *keypair;
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
......@@ -272,6 +275,7 @@ inline struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(ctx->decompressed_point, keypair->private_key, ctx->curve)) {
DBG("uECC_make_key() failed\n");
ctx->err = 11;
}
uECC_compress(ctx->decompressed_point, keypair->compressed_point, ctx->curve);
// DBG("compress failed in create_keypair");
......@@ -279,32 +283,34 @@ inline struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
return keypair;
}
inline int
int
pl_save_key(struct pl_keypair *key, char * filename) {
#ifdef ARDUINO
// not implemented
return 0;
return 1;
#else
FILE *sf;
sf = fopen(filename, "w");
fwrite(key, 1, sizeof(struct pl_keypair), sf);
fclose(sf);
return 0;
#endif
}
inline int
int
pl_load_key(struct pl_keypair *key, char * filename) {
#ifdef ARDUINO
return 0;
return 1;
#else
FILE *lf;
lf = fopen(filename, "r");
fread(key, 1, sizeof(struct pl_keypair), lf);
fclose(lf);
return 0;
#endif
}
......@@ -414,6 +420,7 @@ int pl_inbox_delete(struct pl_ctx *ctx){
}
free(dm->msg);
free(dm);
return 0;
}
int pl_inbox_display(struct pl_ctx *ctx) {
......@@ -423,12 +430,12 @@ int pl_inbox_display(struct pl_ctx *ctx) {
#ifndef ARDUINO
printf("m %u | from: %u to: %u %s \n", list->id , compressed_point_to_addr(list->msg->sender_compressed_point), list->msg->address, list->msg->msg );
#endif
return 1;
return 0;
} else {
#ifndef ARDUINO
printf("DISPlAY: inbox is empty! \n");
#endif
return 0;
return 1;
}
}
......
......@@ -99,6 +99,7 @@ struct pl_ctx
struct list_inbox *inbox;
struct list_inbox *inbox_curr;
uint8_t receiver_compressed_point[ECC_COMPRESSED_SIZE];
uint8_t err;
#ifdef ARDUINO
aes_context aes_ctx;
......
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