Commit acb5cd32 authored by yids's avatar yids

code cleanup and indentation

parent 60db68da
......@@ -23,7 +23,8 @@
* uint8_t *dest destination for the random numbers
* unsigned size number of random bytes to put in dest
*/
int rng (uint8_t *dest, unsigned size) {
int rng (uint8_t *dest, unsigned size)
{
#ifndef ARDUINO
int fd = open("/dev/urandom", O_RDONLY);
if (fd == -1) {
......@@ -77,7 +78,8 @@ inline uint8_t compressed_point_to_addr( uint8_t input[])
* allocate memory for all the stuff in pl_ctx
* returns a pointer to pl_ctx
*/
inline struct pl_ctx * pl_init() {
inline struct pl_ctx * pl_init()
{
struct pl_ctx * ctx;
ctx = (struct pl_ctx *) malloc(sizeof(struct pl_ctx));
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
......@@ -90,7 +92,6 @@ inline struct pl_ctx * pl_init() {
inline int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
{
return 1;
}
......@@ -105,33 +106,23 @@ inline int pl_send_message(struct pl_ctx *ctx) {
memcpy(&ctx->msg->sender_public_key, &ctx->kp->public_key, sizeof(ctx->msg->sender_public_key));
// turn receiver public key into address
ctx->msg->address = compressed_point_to_addr(ctx->receiver_public_key);
// encrypt with NaCl
char temp_plain[PLAIN_MSG_SIZE];
char temp_encrypted[CRYPTED_MSG_SIZE];
int rc;
// if(MSG_SIZE+crypto_box_ZEROBYTES >= MAX_MSG_SIZE) {
// return -2;
// }
memset(temp_plain, '\0', crypto_box_ZEROBYTES);
memcpy(temp_plain + crypto_box_ZEROBYTES, ctx->msg->msg, PLAIN_MSG_SIZE);
//char nonce[crypto_box_NONCEBYTES];
randombytes(ctx->msg->nonce, crypto_box_NONCEBYTES);
rc = crypto_box(temp_encrypted, temp_plain, crypto_box_ZEROBYTES + PLAIN_MSG_SIZE, ctx->msg->nonce, ctx->receiver_public_key, ctx->kp->secret_key);
if( rc != 0 ) {
if( rc != 0 )
return -1;
}
if( is_zero(temp_plain, crypto_box_BOXZEROBYTES) != 0 ) {
if( is_zero(temp_plain, crypto_box_BOXZEROBYTES) != 0 )
return -3;
}
memcpy(ctx->msg->msg, temp_encrypted, CRYPTED_MSG_SIZE);
printf("temp_encrypted: \n %s \n ", temp_encrypted);
// return crypto_box_ZEROBYTES + length - crypto_box_BOXZEROBYTES;
......@@ -146,32 +137,28 @@ int pl_receive_message(struct pl_ctx * ctx)
/* check if message is for us */
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 );
if (ctx->msg->address == list->id)
{
// set keypair to use
ctx->kp = list->kp;
found = 1;
break;
}
}
// exit (and trow away msg) when address not found
if (found == 0) {
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
free(ctx->msg);
return -4;
}
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 0;
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 );
if (ctx->msg->address == list->id){
// set keypair to use
ctx->kp = list->kp;
found = 1;
break;
}
}
// exit (and trow away msg) when address not found
if (found == 0) {
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
free(ctx->msg);
return -4;
}
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 0;
}
//////////////////////////////////////////////////////////////
......@@ -184,98 +171,87 @@ pl_decrypt(struct pl_ctx * ctx, struct pl_pagermessage * msg, char plain[PLAIN_
// TODO: figure out why all the hassle with the zerobytes
#ifndef NOCRYPT
char temp_encrypted[CRYPTED_MSG_SIZE];
char temp_plain[PLAIN_MSG_SIZE];
int rc;
char temp_encrypted[CRYPTED_MSG_SIZE];
char temp_plain[PLAIN_MSG_SIZE];
int rc;
memset(temp_encrypted, '\0', crypto_box_BOXZEROBYTES);
memcpy(temp_encrypted, msg->msg, CRYPTED_MSG_SIZE);
memset(temp_encrypted, '\0', crypto_box_BOXZEROBYTES);
memcpy(temp_encrypted, msg->msg, CRYPTED_MSG_SIZE);
rc = crypto_box_open(temp_plain, temp_encrypted, CRYPTED_MSG_SIZE, msg->nonce, msg->sender_public_key, ctx->kp->secret_key);
rc = crypto_box_open(temp_plain, temp_encrypted, CRYPTED_MSG_SIZE, msg->nonce, msg->sender_public_key, ctx->kp->secret_key);
if( rc != 0 ) {
return -1;
}
if( is_zero(temp_plain, crypto_box_ZEROBYTES) != 0 ) {
return -3;
}
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
if( rc != 0 )
return -1;
if( is_zero(temp_plain, crypto_box_ZEROBYTES) != 0 )
return -3;
// for now store unencrypted in place of encrypted, but should be stored encrypted no?
memcpy(ctx->msg->msg + crypto_box_ZEROBYTES, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
printf("new message: \n %s \n ", plain);
// for now store unencrypted in place of encrypted, but should be stored encrypted no?
memcpy(ctx->msg->msg + crypto_box_ZEROBYTES, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
printf("new message: \n %s \n ", plain);
// return crypto_box_BOXZEROBYTES + MSG_SIZE - crypto_box_ZEROBYTES;
return 0;
return 0;
#endif
}
int is_zero( const char *data, int len )
{
int i;
int rc;
rc = 0;
for(i = 0; i < len; ++i) {
rc |= data[i];
}
return rc;
int i;
int rc;
rc = 0;
for(i = 0; i < len; ++i) {
rc |= data[i];
}
return rc;
}
struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx) {
struct pl_keypair * pl_create_keypair(struct pl_ctx *ctx)
{
struct pl_keypair *keypair;
// arduino doesn't have RNG, so can't be used for generating keys
#ifdef ARDUINO
#ifdef ARDUINO
return NULL;
#else
#else
keypair = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
crypto_box_keypair(keypair->public_key, keypair->secret_key);
return keypair;
#endif
#endif
}
int
pl_save_key(struct pl_keypair *key, char * filename) {
int pl_save_key(struct pl_keypair *key, char * filename)
{
#ifdef ARDUINO
// not implemented
return 1;
#else
FILE *sf;
sf = fopen(filename, "w");
fwrite(key, 1, sizeof(struct pl_keypair), sf);
fclose(sf);
return 0;
#endif
}
int
pl_load_key(struct pl_keypair *key, char * filename) {
int pl_load_key(struct pl_keypair *key, char * filename)
{
#ifdef ARDUINO
return 1;
#else
FILE *lf;
lf = fopen(filename, "r");
fread(key, 1, sizeof(struct pl_keypair), lf);
fclose(lf);
return 0;
#endif
}
int 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++;
#ifndef ARDUINO
printf("AP: count: %u", ctx->msgcount);
#endif
......@@ -291,19 +267,17 @@ int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
ctx->inbox = ni;
ctx->inbox_curr = ni;
}
else
{
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);
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;
......@@ -312,10 +286,10 @@ int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg){
}
return 1;
}
inline int pl_inbox_next(struct pl_ctx *ctx){
if ( ctx->inbox_curr != NULL && ctx->inbox_curr->next != NULL)
{
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;
}
......@@ -327,10 +301,9 @@ inline int pl_inbox_next(struct pl_ctx *ctx){
}
}
int pl_inbox_prev(struct pl_ctx *ctx){
if (ctx->inbox_curr != NULL && ctx->inbox_curr->prev != NULL)
{
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;
}
......@@ -341,44 +314,39 @@ int pl_inbox_prev(struct pl_ctx *ctx){
return 0;
}
}
/*
* delete currently selected message
*/
int pl_inbox_delete(struct pl_ctx *ctx){
if (ctx->inbox_curr == NULL | ctx->inbox == NULL) {
// make a inbox
return 1;
}
int pl_inbox_delete(struct pl_ctx *ctx)
{
if (ctx->inbox_curr == NULL | ctx->inbox == NULL)
return 1; // make a inbox
struct list_inbox * dm;
dm = ctx->inbox_curr;
if (dm->next != NULL && dm->prev != NULL)
{ // there both others
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)
{
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)
{
else if (dm->next == NULL && dm->prev != NULL){
dm->prev->next = NULL;
} else if (dm->next != NULL && dm->prev == NULL)
{
}
else if (dm->next != NULL && dm->prev == NULL){
dm->next->prev = NULL;
}
free(dm->msg);
free(dm);
return 0;
free(dm->msg);
free(dm);
return 0;
}
int panic(struct pl_ctx *ctx)
{
// clean decryptbuffer
......@@ -387,6 +355,7 @@ int panic(struct pl_ctx *ctx)
// delete all keys
return -1;
}
/*
write_screen(struct pl_ctx *ctx)
{
......@@ -394,36 +363,38 @@ write_screen(struct pl_ctx *ctx)
}
*/
int pl_inbox_display(struct pl_ctx *ctx) {
int pl_inbox_display(struct pl_ctx *ctx)
{
char decryptbuffer[100];
if (ctx->inbox_curr != NULL) {
struct list_inbox * list;
list = ctx->inbox_curr;
pl_decrypt(ctx,list->msg,decryptbuffer);
struct list_inbox * list;
list = ctx->inbox_curr;
pl_decrypt(ctx,list->msg,decryptbuffer);
#ifndef ARDUINO
printf("m %u | from: %u to: %u %s \n", list->id , compressed_point_to_addr(list->msg->sender_public_key), list->msg->address, ctx->decryptbuffer );
printf("m %u | from: %u to: %u %s \n", list->id , compressed_point_to_addr(list->msg->sender_public_key), list->msg->address, ctx->decryptbuffer );
#endif
return 0;
}
return 0;
}
}
int pl_inbox_display_all(struct pl_ctx *ctx) {
struct list_inbox * list;
int i = 0;
int pl_inbox_display_all(struct pl_ctx *ctx)
{
struct list_inbox * list;
int i = 0;
#ifndef ARDUINO
printf("messages: \n ");
printf("messages: \n ");
#endif
for(list = ctx->inbox; list != NULL; list = list->next) {
for(list = ctx->inbox; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("m %u | from: %u to: %u %s \n", list->id , compressed_point_to_addr(list->msg->sender_public_key), list->msg->address, list->msg->msg );
printf("m %u | from: %u to: %u %s \n", list->id , compressed_point_to_addr(list->msg->sender_public_key), list->msg->address, list->msg->msg );
#endif
i++;
}
i++; // <- this doesnt do anything does it?
}
}
int 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,36 +404,32 @@ int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key){
ni->id = compressed_point_to_addr(key->public_key);
ni->kp = key;
// check if list exists
if (list == NULL) {
// last = ni;
// list = ni;
ctx->keypairs = ni;
}
else
{
else {
// walk to end of list
for(1; 1; list = list->next) {
// printf("walk: %u \n ", list->next);
if (list->next == NULL) break;
if (list->next == NULL)
break;
}
list->next = ni;
}
}
int pl_print_keylist(struct pl_ctx *ctx) {
struct list_kp * list;
int pl_print_keylist(struct pl_ctx *ctx)
{
struct list_kp * list;
#ifndef ARDUINO
printf("the list of loaded keys: \n ");
printf("the list of loaded keys: \n ");
#endif
for(list = ctx->keypairs; list != NULL; list = list->next) {
for(list = ctx->keypairs; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("address in list: %u \n", list->id );
printf("address in list: %u \n", list->id );
#endif
}
}
}
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