Commit 5b50bcd9 authored by yids's avatar yids

Merge branch 'master' of 185.52.224.4:yids/avr-pager

parents b9c990a7 e9bd56fc
CC=colorgcc
CFLAGS="-I ../libs"
CFLAGS="-I ../libs -DDEBUG"
all: test rx-test tx-test keygen
......
......@@ -13,6 +13,8 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
//#include "packets.h"
#include "../libs/pagerlib/pagerlib.h"
......@@ -21,86 +23,87 @@
#endif
int main() {
struct pl_keypair *receiver, *sender;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
struct pl_keypair *kp;
kp = NULL;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
// initialise the pager
struct pl_ctx * context;
/*
if ( pl_init(context) < 1 )
{
DBG("pagerlib initialized \n");
}
*/
// initialise the pager
struct pl_ctx * context;
context = pl_init();
// sended_msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(sender, 9, sizeof(struct pl_keypair));
context = pl_init();
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(receiver, 2, sizeof(struct pl_keypair));
int f = 0;
while ( f < 2 ) {
f++;
/* create keypairs */
kp = pl_create_keypair(context);
pl_load_key_in_list(context, kp);
// set key to use for sending the message (FIXME)
context->kp = kp;
/* create keypairs */
pl_create_keypair(context, sender);
pl_save_key(sender,"sender_test.keypair");
pl_create_keypair(context, receiver);
pl_save_key(receiver,"receiver_test.keypair");
// load the message to send
memcpy(context->msg->msg, clear_message, MSG_SIZE);
//switch to sender key
pl_load_key(sender, "sender_test.keypair");
pl_load_key(receiver, "receiver_test.keypair");
//to who to send the message to (get from kp)
memcpy(&context->receiver_compressed_point, &kp->compressed_point, sizeof(context->receiver_compressed_point));
pl_load_key_in_list(context, sender);
pl_load_key_in_list(context, receiver);
pl_send_message(context);
// print list of keypairs
pl_print_keylist(context);
// ------------------------------------------------- //
/* receive the msg */
if ( pl_receive_message(context) == 1) {
printf("failed to receive this message ! (exit 1) \n");
} else {
printf("recv success! \n");
// printf(" Message! \n to: %u from: %u \n the decrypted message: %s \n", context->msg->address, compressed_point_to_addr(context->msg->sender_compressed_point), context->msg->msg);
}
// set key to use for sending the message
context->kp = sender;
/*
DBG("\n sender.keypair");
DBM("sender", sizeof(struct pl_keypair), sender);
DBM("receiver", sizeof(struct pl_keypair), receiver);
DBM("context->kp (sender)", sizeof(struct pl_keypair), context->kp);
DBM("context->kp->private_key (sender)", sizeof(context->kp->private_key), &context->kp->private_key);
DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
DBM("context->receiver_compressed_point",sizeof(context->receiver_compressed_point) , &context->receiver_compressed_point);
*/
// load the message to send
memcpy(context->msg->msg, clear_message, MSG_SIZE);
//
memcpy(&context->receiver_compressed_point, &receiver->compressed_point, sizeof(context->receiver_compressed_point));
pl_send_message(context);
/*
DBG("\n sender.keypair");
DBM("sender", sizeof(struct pl_keypair), sender);
DBM("receiver", sizeof(struct pl_keypair), receiver);
DBM("context->kp (sender)", sizeof(struct pl_keypair), context->kp);
DBM("context->kp->private_key (sender)", sizeof(context->kp->private_key), &context->kp->private_key);
DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
DBM("context->receiver_compressed_point",sizeof(context->receiver_compressed_point) , &context->receiver_compressed_point);
DBM("context->kp (receiver)", sizeof(struct pl_keypair), context->kp);
DBM("context->kp->private_key (receiver)", sizeof(context->kp->private_key), &context->kp->private_key);
context->kp = receiver;
// DBM("context->kp (receiver)", sizeof(struct pl_keypair), context->kp);
// DBM("context->kp->private_key (receiver)", sizeof(context->kp->private_key), &context->kp->private_key);
*/
context->kp = kp;
if ( pl_receive_message(context) == 1) {
printf("failed to receive this message ! (exit 1)");
if (pl_receive_message(context) == 1) {
printf("OK: \n");
} else {
printf(" Message! \n to: %u from: %u \n the decrypted message: %s \n", context->msg->address, compressed_point_to_addr(context->msg->sender_compressed_point), context->msg->msg);
printf("failed to receive this message ! (exit 1)");
}
// printf("\n ----------------------------\n");
// dump_buffer(sizeof(struct pl_pagermessage), sended_msg);
//printf("\n The message that decrypted: \n");
// pl_receive_message(receiver, sended_msg, &decrypted_message, &shared_secret, &decompressed_point);
//printf("the message: %s", &decrypted_message);
//printf("\n ----------------------------\n");
return 0;
// pl_inbox_display_all(context);
while (pl_inbox_next(context))
{
pl_inbox_display(context);
}
while (pl_inbox_prev(context)) {
pl_inbox_display(context);
}
while (pl_inbox_next(context) && context->inbox->prev != NULL && context->inbox->prev->id > 9)
{
pl_inbox_delete(context);
}
while (pl_inbox_prev(context)) {
pl_inbox_display(context);
}
// pl_inbox_delete(context);
pl_inbox_display(context);
}
return 0;
}
......@@ -10,6 +10,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
......@@ -55,7 +58,7 @@ int rng (uint8_t *dest, unsigned size) {
* takes an compressed point and turns it into an address
* should become a proper hash function one day
*/
uint32_t compressed_point_to_addr( uint8_t input[])
inline uint32_t compressed_point_to_addr( uint8_t input[])
{
uint32_t hashval;
int i = 0;
......@@ -80,9 +83,13 @@ inline struct pl_ctx * pl_init() {
ctx->curve = CURVE;
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(ctx->msg, 7, sizeof(struct pl_pagermessage));
ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(ctx->kp, 9, sizeof(struct pl_keypair));
// ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
// memset(ctx->kp, 9, sizeof(struct pl_keypair));
ctx->kp = NULL;
// ctx->receiver_compressed_point;
// ctx->keypairs = (struct list_kp *) malloc( sizeof(struct list_kp) );
ctx->keypairs = NULL;
ctx->inbox = NULL;
uECC_set_rng(rng);
return ctx;
}
......@@ -103,7 +110,7 @@ inline int pl_send_message(struct pl_ctx *ctx) {
memset(ctx->shared_secret, 7, sizeof(ctx->shared_secret));
/* create a random iv */
rng(&ctx->msg->iv, sizeof(ctx->msg->iv));
rng((char *)&ctx->msg->iv, sizeof(ctx->msg->iv));
// copy compressed point into pager message
memcpy(&ctx->msg->sender_compressed_point, &ctx->kp->compressed_point, sizeof(ctx->msg->sender_compressed_point));
......@@ -111,7 +118,7 @@ inline int pl_send_message(struct pl_ctx *ctx) {
//DBG("Sending message \n");
#ifndef NOCRYPT
/* 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);
......@@ -121,7 +128,7 @@ 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(&ctx->decompressed_point, &ctx->kp->private_key, &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");
return 1;
}
......@@ -139,7 +146,7 @@ inline int pl_send_message(struct pl_ctx *ctx) {
mbedtls_aes_init( &ctx->aes_ctx );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &ctx->aes_ctx, &ctx->shared_secret, AES_KEYSIZE );
mbedtls_aes_setkey_enc( &ctx->aes_ctx, (char *)&ctx->shared_secret, AES_KEYSIZE );
char cmsg[MSG_SIZE];
memcpy(&cmsg, &ctx->msg->msg, MSG_SIZE);
......@@ -149,7 +156,7 @@ inline int pl_send_message(struct pl_ctx *ctx) {
// DBM("iv in pl_send_message before crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, civ, &cmsg, &ctx->msg->msg );
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, civ, (char *)&cmsg, (char *)&ctx->msg->msg );
mbedtls_aes_free (&ctx->aes_ctx);
// DBG("message to send: %s \n ", ctx->msg);
#endif
......@@ -181,7 +188,7 @@ inline int pl_receive_message(struct pl_ctx * ctx)
struct list_kp * list;
int found = 0;
for(list = ctx->keypairs; list != NULL; list = list->next) {
printf("\n address in msg: %u address in list: %u \n", ctx->msg->address, list->id );
// printf("\n address in msg: %u address in list: %u \n", ctx->msg->address, list->id );
if (ctx->msg->address == list->id)
{
// set keypair to use
......@@ -190,15 +197,15 @@ inline int pl_receive_message(struct pl_ctx * ctx)
}
}
// exit when address not found
if (found == 0) return 1;
if (found == 0) return 0;
/* 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");
}
// DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
......@@ -218,37 +225,51 @@ inline int pl_receive_message(struct pl_ctx * ctx)
mbedtls_aes_init( &ctx->aes_ctx );
//DBM("iv in pl_receive_message", sizeof(ctx->msg->iv), &ctx->msg->iv);
mbedtls_aes_setkey_dec( &ctx->aes_ctx, &ctx->shared_secret, AES_KEYSIZE );
mbedtls_aes_setkey_dec( &ctx->aes_ctx, (char *)&ctx->shared_secret, AES_KEYSIZE );
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, ctx->msg->iv, &ctx->msg->msg, &ctx->clear_message );
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, ctx->msg->iv, (char *)&ctx->msg->msg, (char *)&ctx->clear_message );
mbedtls_aes_free ( &ctx->aes_ctx );
memcpy(ctx->msg->msg, ctx->clear_message, MSG_SIZE);
#endif
#endif
/*
DBM("msg->msg in pl_receive_message after crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
DBM("iv in pl_receive_message after crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
*/
// message decrypted, now store it in the inbox
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;
}
inline int pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
inline struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
struct pl_keypair *keypair;
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
// memset(keypair->private_key, 4, sizeof(keypair->private_key));
// memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
keypair = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(&ctx->decompressed_point, &keypair->private_key, ctx->curve)) {
if (!uECC_make_key(ctx->decompressed_point, keypair->private_key, ctx->curve)) {
DBG("uECC_make_key() failed\n");
}
uECC_compress(&ctx->decompressed_point, &keypair->compressed_point, ctx->curve);
uECC_compress(ctx->decompressed_point, keypair->compressed_point, ctx->curve);
// DBG("compress failed in create_keypair");
return 0;
return keypair;
}
inline int
......@@ -280,12 +301,134 @@ pl_load_key(struct pl_keypair *key, char * filename) {
#endif
}
pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
struct list_inbox *list, *ni, *last, *listprev;
list = ctx->inbox;
ctx->msgcount++;
printf("AP: count: %u", ctx->msgcount);
// make new list item
ni = (struct list_inbox *) malloc( sizeof(struct list_inbox) );
ni->id = ctx->msgcount;
ni->next = NULL;
ni->status = MSG_NEW;
ni->msg = msg;
// check if list exists
if (list == NULL) {
ctx->inbox = ni;
ctx->inbox_curr = ni;
}
else
{
// walk to end of list
//
while ( 1 )
{
listprev = list;
if (list->next != NULL) {
list = list->next;
// printf ("AP: middle item (list): %u listprev: %u \n", list->id, listprev->id);
} else {
// printf ("AP: last item (list): %u listprev: %u \n", list->id, listprev->id);
ni->prev = listprev;
list->next = ni;
break;
}
}
}
}
inline int pl_inbox_next(struct pl_ctx *ctx){
if ( ctx->inbox_curr != NULL && ctx->inbox_curr->next != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->next;
return 1;
}
else {
printf("NEXT: already at last item \n");
return 0;
}
}
int pl_inbox_prev(struct pl_ctx *ctx){
if (ctx->inbox_curr != NULL && ctx->inbox_curr->prev != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->prev;
return 1;
}
else {
printf("PREV: already at first item \n");
return 0;
}
}
/*
* delete currently selected message
*/
int pl_inbox_delete(struct pl_ctx *ctx){
if (ctx->inbox_curr == NULL | ctx->inbox == NULL) {
printf("stomding");
exit(1);
}
struct list_inbox * dm;
dm = ctx->inbox_curr;
if (dm->next != NULL && dm->prev != NULL)
{ // there both others
// link prev and next with each other
dm->next->prev = dm->prev;
dm->prev->next = dm->next;
}
else if (dm->next == NULL && dm->prev == NULL)
{
// no others
ctx->inbox = NULL;
ctx->inbox_curr = NULL;
}
else if (dm->next == NULL && dm->prev != NULL)
{
dm->prev->next = NULL;
} else if (dm->next != NULL && dm->prev == NULL)
{
dm->next->prev = NULL;
}
free(dm->msg);
free(dm);
}
int pl_inbox_display(struct pl_ctx *ctx) {
if (ctx->inbox_curr != NULL) {
struct list_inbox * list;
list = ctx->inbox_curr;
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 );
return 1;
} else {
printf("DISPlAY: inbox is empty! \n");
return 0;
}
}
int pl_inbox_display_all(struct pl_ctx *ctx) {
struct list_inbox * list;
int i = 0;
printf("messages: \n ");
for(list = ctx->inbox; list != NULL; list = list->next) {
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 );
i++;
}
}
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
struct list_kp *list, *ni, *last;
list = ctx->keypairs;
// make new list item
// make new list item
ni = (struct list_kp *) malloc( sizeof(struct list_kp) );
ni->next = NULL;
ni->id = compressed_point_to_addr(key->compressed_point);
......@@ -311,7 +454,8 @@ pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
}
pl_print_keylist(struct pl_ctx *ctx) {
int pl_print_keylist(struct pl_ctx *ctx) {
struct list_kp * list;
printf("the list of loaded keys: \n ");
for(list = ctx->keypairs; list != NULL; list = list->next) {
......
......@@ -7,7 +7,7 @@ extern "C"
//#define NOCRYPT
#define DEBUG
#if defined(__AVR_ATmega328P__)
#define ARDUINO
......@@ -22,6 +22,8 @@ extern "C"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#include <unistd.h>
#include <ctype.h>
#endif
#ifdef DEBUG
......@@ -61,9 +63,11 @@ typedef struct list_kp {
/*
* the inbox
*/
#define MSG_NEW 1
#define MSG_READ 2
typedef struct list_inbox {
int status; // 1 unread 2 read
int id;
struct pl_pagermessage *msg;
struct list_inbox *prev;
struct list_inbox *next;
......@@ -84,13 +88,15 @@ struct pl_ctx
char decompressed_point[64];
// contains the message that will be send or is received
char clear_message[MSG_SIZE];
// my (currently used) keypair
// my (currently used ) keypair
struct pl_keypair *kp;
// keypair list (all available keypairs)
struct list_kp *keypairs;
// place to store messages to be sent or received
struct pl_pagermessage *msg;
int msgcount;
struct list_inbox *inbox;
struct list_inbox *inbox_curr;
uint8_t receiver_compressed_point[ECC_COMPRESSED_SIZE];
#ifdef ARDUINO
......@@ -112,15 +118,22 @@ mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
struct pl_ctx * pl_init();
int pl_create_keypair(struct pl_ctx*, struct pl_keypair *);
struct pl_keypair * pl_create_keypair(struct pl_ctx*);
int pl_send_message(struct pl_ctx *);
int pl_receive_message(struct pl_ctx*);
int pl_save_key(struct pl_keypair *key, char * filename);
int pl_load_key(struct pl_keypair *key, char * filename);
int pl_print_keylist(struct pl_ctx *ctx);
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key);
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg);
uint32_t compressed_point_to_addr( uint8_t input[]);
int pl_inbox_display(struct pl_ctx *ctx);
int pl_inbox_next(struct pl_ctx *ctx);
int pl_inbox_prev(struct pl_ctx *ctx);
int pl_inbox_delete(struct pl_ctx *ctx);
#ifdef __cplusplus
} /* end of extern "C" */
......
......@@ -21,6 +21,7 @@ const char b64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#include <ctype.h>
#endif
......
#define DBG
// common.h
void dump_buffer(unsigned int , const unsigned char* );
......
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