Commit 61712052 authored by yids's avatar yids

changed some of the keyloading functions and some other stuff

parent 0d46deb8
......@@ -19,10 +19,12 @@ int main ()
rx_ctx = pl_init();
/*load keypair */
rx_kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
pl_load_key(rx_kp, "rx");
pl_load_key_in_list(rx_ctx, rx_kp);
rx_ctx->kp = rx_kp;
// rx_kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
// rx_kp = pl_load_key("rx");
// pl_load_key(rx_kp, "rx");
rx_ctx->kp = pl_load_key("rx");
pl_load_key_in_list(rx_ctx, rx_ctx->kp);
/* get message from stdin */
struct pl_pagermessage * message = NULL;
......
......@@ -15,24 +15,20 @@ int main()
struct pl_keypair *tx_kp;
struct pl_ctx *tx_ctx;
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
char *rx_pubkey;
/* init */
tx_kp = NULL;
tx_ctx = pl_init();
/* load keypair */
tx_kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
pl_load_key(tx_kp, "tx");
tx_ctx->kp = tx_kp;
tx_ctx->kp = pl_load_key("tx");
/*load public key of receiver*/
char *rx_pubkey;
rx_pubkey = malloc(crypto_box_PUBLICKEYBYTES);
pl_load_public_key(rx_pubkey, "rx.pub");
pl_load_public_key(tx_ctx, "rx.pub");
memcpy(tx_ctx->msg->msg, clear_message, MSG_SIZE); // copy clear msg into context
memcpy(&tx_ctx->receiver_public_key, rx_pubkey, sizeof(tx_ctx->receiver_public_key)); // copy rx pubkey into context
switch(pl_send_message(tx_ctx)) { // send msg
case 0:
printf("SEND: OK! \n");
......
......@@ -234,16 +234,18 @@ int pl_save_key(struct pl_keypair *key, char *filename)
#endif
}
int pl_load_key(struct pl_keypair *key, char *filename)
struct pl_keypair * pl_load_key(char *filename)
{
struct pl_keypair *keypair;
#ifdef ARDUINO
return 1;
#else
keypair = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
FILE *lf;
lf = fopen(filename, "r");
fread(key, 1, sizeof(struct pl_keypair), lf);
fread(keypair, 1, sizeof(struct pl_keypair), lf);
fclose(lf);
return 0;
return keypair;
#endif
}
......@@ -260,19 +262,63 @@ int pl_save_public_key(char *pubkey, char *filename)
#endif
}
int pl_load_public_key(char pubkey[crypto_box_PUBLICKEYBYTES], char *filename)
/*
* Load a public key from file and put it in the context so you can encrypt messages to it
*/
int pl_load_public_key(struct pl_ctx *ctx, char *filename)
{
#ifdef ARDUINO
return 1;
#else
char *public_key;
public_key = malloc(crypto_box_PUBLICKEYBYTES);
FILE *lf;
lf = fopen(filename, "r");
fread(pubkey, 1, crypto_box_PUBLICKEYBYTES, lf);
fread(public_key, 1, crypto_box_PUBLICKEYBYTES, lf);
fclose(lf);
memcpy(&ctx->receiver_public_key, public_key, sizeof(ctx->receiver_public_key));
return 0;
}
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
ni = (struct list_kp *) malloc( sizeof(struct list_kp) );
ni->next = NULL;
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 {
// walk to end of list
for(1; 1; list = list->next) {
// printf("walk: %u \n ", list->next);
if (list->next == NULL)
break;
}
list->next = ni;
}
}
int pl_print_keylist(struct pl_ctx *ctx)
{
struct list_kp * list;
#ifndef ARDUINO
printf("the list of loaded keys: \n ");
#endif
for(list = ctx->keypairs; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("address in list: %u \n", list->id );
#endif
}
}
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg)
{
struct list_inbox *list, *ni, *last, *listprev;
......@@ -419,45 +465,4 @@ int pl_inbox_display_all(struct pl_ctx *ctx)
}
}
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
ni = (struct list_kp *) malloc( sizeof(struct list_kp) );
ni->next = NULL;
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 {
// walk to end of list
for(1; 1; list = list->next) {
// printf("walk: %u \n ", list->next);
if (list->next == NULL)
break;
}
list->next = ni;
}
}
int pl_print_keylist(struct pl_ctx *ctx)
{
struct list_kp * list;
#ifndef ARDUINO
printf("the list of loaded keys: \n ");
#endif
for(list = ctx->keypairs; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("address in list: %u \n", list->id );
#endif
}
}
......@@ -120,9 +120,10 @@ 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);
struct pl_keypair * pl_load_key(char *filename);
int pl_save_public_key(char pubkey[crypto_box_PUBLICKEYBYTES], char *filename);
int pl_load_public_key(char pubkey[crypto_box_PUBLICKEYBYTES], char *filename);
int pl_load_public_key(struct pl_ctx *ctx, char *filename);
//int pl_load_public_key(char pubkey[crypto_box_PUBLICKEYBYTES], 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);
......
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