Commit e9bd56fc authored by hark's avatar hark

no complains from compiler anymore

and working inbox
parent 6357130a
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"
......@@ -31,7 +33,7 @@ int main() {
context = pl_init();
int f = 0;
while ( f < 10 ) {
while ( f < 2 ) {
f++;
/* create keypairs */
kp = pl_create_keypair(context);
......@@ -58,7 +60,7 @@ int main() {
}
/*
DBG("\n sender.keypair");
......@@ -73,51 +75,35 @@ int main() {
*/
int i = 0;
while ( i < 10 ) {
i++;
context->kp = kp;
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)");
}
}
//pl_inbox_display_all(context);
// pl_inbox_display_all(context);
i = 0;
while ( i < 20)
{
if (pl_inbox_next(context)) {
while (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;
}
}
pl_print_keylist(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;
......@@ -83,7 +86,7 @@ inline struct pl_ctx * pl_init() {
// 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;
......@@ -107,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));
......@@ -115,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);
......@@ -125,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;
}
......@@ -143,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);
......@@ -153,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
......@@ -194,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);
......@@ -222,17 +225,19 @@ 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);
......@@ -242,6 +247,7 @@ ctx->msg = NULL;
// make space for the next one
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
return 1;
}
......@@ -257,10 +263,10 @@ 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)) {
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 keypair;
......@@ -295,16 +301,19 @@ pl_load_key(struct pl_keypair *key, char * filename) {
#endif
}
pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
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;
......@@ -313,45 +322,53 @@ pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
else
{
// walk to end of list
for(1; listprev = list; list = list->next) {
// printf("walk: %u \n ", list->next);
if (list->next == NULL) break;
//
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;
}
}
list->prev = listprev;
list->next = ni;
}
}
inline int pl_inbox_next(struct pl_ctx *ctx){
pl_inbox_next(struct pl_ctx *ctx){
if ( ctx->inbox_curr->next != NULL)
if ( ctx->inbox_curr != NULL && ctx->inbox_curr->next != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->next;
return 1;
}
else {
printf("already at first item");
printf("NEXT: already at last item \n");
return 0;
}
}
pl_inbox_prev(struct pl_ctx *ctx){
int pl_inbox_prev(struct pl_ctx *ctx){
if (ctx->inbox_curr->prev != NULL)
if (ctx->inbox_curr != NULL && ctx->inbox_curr->prev != NULL)
{
ctx->inbox_curr = ctx->inbox_curr->prev;
return 0;
return 1;
}
else {
printf("already at last item \n");
return 1;
printf("PREV: already at first item \n");
return 0;
}
}
/*
* delete currently selected message
*/
pl_inbox_delete(struct pl_ctx *ctx){
int pl_inbox_delete(struct pl_ctx *ctx){
if (ctx->inbox_curr == NULL | ctx->inbox == NULL) {
printf("stomding");
......@@ -380,28 +397,33 @@ pl_inbox_delete(struct pl_ctx *ctx){
{
dm->next->prev = NULL;
}
// free(dm->msg);
// free(dm);
free(dm->msg);
free(dm);
}
pl_inbox_display(struct pl_ctx *ctx) {
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", 1 , compressed_point_to_addr(list->msg->sender_compressed_point), list->msg->address, list->msg->msg );
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;
}
}
pl_inbox_display_all(struct pl_ctx *ctx) {
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", i , compressed_point_to_addr(list->msg->sender_compressed_point), list->msg->address, list->msg->msg );
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++;
}
}
pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
struct list_kp *list, *ni, *last;
list = ctx->keypairs;
......@@ -433,7 +455,7 @@ 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
......@@ -65,6 +67,7 @@ typedef struct list_kp {
#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;
......@@ -91,10 +94,10 @@ struct pl_ctx
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];
uint8_t receiver_compressed_point[ECC_COMPRESSED_SIZE];
#ifdef ARDUINO
aes_context aes_ctx;
......@@ -122,11 +125,15 @@ 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