Commit cea680fd authored by hark's avatar hark

some inbox things

parent 6ffef450
......@@ -21,46 +21,50 @@
#endif
int main() {
struct pl_keypair *receiver, *sender;
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");
}
*/
context = pl_init(context);
/*
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(sender, 9, sizeof(struct pl_keypair));
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(receiver, 2, sizeof(struct pl_keypair));
*/
int f = 0;
while ( 1 ) {
f++;
if (f == 10) break;
/* create keypairs */
kp = pl_create_keypair(context);
pl_load_key_in_list(context, 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");
//switch to sender key
pl_load_key(sender, "sender_test.keypair");
pl_load_key(receiver, "receiver_test.keypair");
pl_load_key_in_list(context, sender);
pl_load_key_in_list(context, receiver);
*/
// print list of keypairs
pl_print_keylist(context);
// ------------------------------------------------- //
// ------------------------------------------------- //
// set key to use for sending the message
context->kp = sender;
// set key to use for sending the message (FIXME)
context->kp = kp;
/*
DBG("\n sender.keypair");
......@@ -71,16 +75,21 @@ int main() {
DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
DBM("context->receiver_compressed_point",sizeof(context->receiver_compressed_point) , &context->receiver_compressed_point);
*/
int i = 0;
while ( 1 ) {
i++;
if (i == 10) break;
// 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));
memcpy(&context->receiver_compressed_point, &kp->compressed_point, sizeof(context->receiver_compressed_point));
pl_send_message(context);
context->kp = receiver;
context->kp = kp;
// 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);
......@@ -89,17 +98,40 @@ int main() {
printf("failed to receive this message ! (exit 1)");
} 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(" 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("\n ----------------------------\n");
// dump_buffer(sizeof(struct pl_pagermessage), sended_msg);
}
pl_print_keylist(context);
//pl_inbox_display_all(context);
i = 0;
while ( i < 20)
{
if (pl_inbox_next(context)) {
pl_inbox_display(context);
pl_inbox_delete(context);
pl_send_message(context);
pl_inbox_display(context);
pl_inbox_delete(context);
pl_inbox_display(context);
pl_inbox_delete(context);
pl_inbox_display(context);
}
else
{
break;
}
}
//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;
return 0;
}
......@@ -77,9 +77,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->keypairs = (struct list_kp *) malloc( sizeof(struct list_kp) );
ctx->keypairs = NULL;
ctx->inbox = NULL;
uECC_set_rng(rng);
return ctx;
}
......@@ -178,7 +182,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
......@@ -226,17 +230,28 @@ inline int pl_receive_message(struct pl_ctx * ctx)
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));
}
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)) {
......@@ -245,7 +260,7 @@ inline int pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
uECC_compress(&ctx->decompressed_point, &keypair->compressed_point, ctx->curve);
// DBG("compress failed in create_keypair");
return 0;
return keypair;
}
inline int
......@@ -277,12 +292,118 @@ pl_load_key(struct pl_keypair *key, char * filename) {
#endif
}
pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
struct list_inbox *list, *ni, *last, *listprev;
list = ctx->inbox;
// make new list item
ni = (struct list_inbox *) malloc( sizeof(struct list_inbox) );
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
for(1; listprev = list; list = list->next) {
// printf("walk: %u \n ", list->next);
if (list->next == NULL) break;
}
list->prev = listprev;
list->next = ni;
}
}
pl_inbox_next(struct pl_ctx *ctx){
if ( ctx->inbox_curr->next != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->next;
return 1;
}
else {
printf("already at first item");
return 0;
}
}
pl_inbox_prev(struct pl_ctx *ctx){
if (ctx->inbox_curr->prev != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->prev;
return 0;
}
else {
printf("already at last item \n");
return 1;
}
}
/*
* delete currently selected message
*/
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);
}
pl_inbox_display(struct pl_ctx *ctx) {
struct list_inbox * list;
list = ctx->inbox_curr;
printf("m %u | from: %u to: %u %s \n", 1 , compressed_point_to_addr(list->msg->sender_compressed_point), list->msg->address, list->msg->msg );
}
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", i , compressed_point_to_addr(list->msg->sender_compressed_point), list->msg->address, list->msg->msg );
i++;
}
}
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);
......
......@@ -61,7 +61,8 @@ typedef struct list_kp {
/*
* the inbox
*/
#define MSG_NEW 1
#define MSG_READ 2
typedef struct list_inbox {
int status; // 1 unread 2 read
struct pl_pagermessage *msg;
......@@ -90,7 +91,8 @@ struct pl_ctx
struct list_kp *keypairs;
// place to store messages to be sent or received
struct pl_pagermessage *msg;
struct list_inbox *inbox;
struct list_inbox *inbox_curr;
uint8_t receiver_compressed_point[ECC_COMPRESSED_SIZE];
#ifdef ARDUINO
......@@ -112,7 +114,7 @@ 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*);
......@@ -121,6 +123,9 @@ int pl_save_key(struct pl_keypair *key, char * filename);
int pl_load_key(struct pl_keypair *key, char * filename);
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);
#ifdef __cplusplus
} /* end of extern "C" */
......
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