Commit ee8f70e3 authored by yids's avatar yids

yeah some things

parent 61712052
#include "../libs/micro-ecc/uECC.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libs/pagerlib/pagerlib.h"
#ifndef uECC_TEST_NUMBER_OF_ITERATIONS
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
int main ()
{
struct pl_ctx *rx_ctx;
struct pl_keypair *rx_kp;
rx_kp = NULL;
......@@ -19,12 +16,9 @@ int main ()
rx_ctx = pl_init();
/*load keypair */
// 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(rx_ctx, "rx");
pl_load_key_in_list(rx_ctx, rx_ctx->kp);
pl_print_keylist(rx_ctx);
/* get message from stdin */
struct pl_pagermessage * message = NULL;
......
#include "../libs/micro-ecc/uECC.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libs/pagerlib/pagerlib.h"
#ifndef uECC_TEST_NUMBER_OF_ITERATIONS
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
void load_a_lot_of_keys(struct pl_ctx *ctx)
{
char *pubkey;
for(int i=1; i<6; i++){
pubkey = NULL;
char filename[5];
sprintf(filename, "%d.pub",i);
printf("loading pubkey: %s\n", filename);
pubkey = pl_load_public_key(filename);
pl_load_public_key_in_list(ctx, pubkey);
}
}
int main()
{
struct pl_keypair *tx_kp;
......@@ -22,11 +28,15 @@ int main()
tx_ctx = pl_init();
/* load keypair */
tx_ctx->kp = pl_load_key("tx");
pl_load_key(tx_ctx, "tx");
/*load public key of receiver*/
pl_load_public_key(tx_ctx, "rx.pub");
char *pubkey;
pubkey = pl_load_public_key("rx.pub");
pl_print_public_keylist(tx_ctx);
pl_set_receiver(tx_ctx, pubkey);
memcpy(tx_ctx->msg->msg, clear_message, MSG_SIZE); // copy clear msg into context
switch(pl_send_message(tx_ctx)) { // send msg
......
......@@ -56,7 +56,7 @@ int rng (uint8_t *dest, unsigned size)
}
/*
* takes an compressed point and turns it into an address
* takes a compressed point and turns it into an address
* FIXME: should become a proper hash function
*/
inline uint8_t compressed_point_to_addr( uint8_t input[])
......@@ -83,16 +83,17 @@ 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));
memset(ctx->msg, 7, sizeof(struct pl_pagermessage));
ctx->kp = NULL;
// memset(ctx->msg, 7, sizeof(struct pl_pagermessage)); this is for testing no?//
ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));; // why NULL?
ctx->keypairs = NULL;
ctx->inbox = NULL;
return ctx;
}
inline int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
inline int pl_set_receiver(struct pl_ctx *ctx, char *public_key)
{
return 1;
memcpy(ctx->receiver_public_key, public_key, crypto_box_PUBLICKEYBYTES);
return 0;
}
/*
......@@ -124,7 +125,7 @@ inline int pl_send_message(struct pl_ctx *ctx) {
return -3;
memcpy(ctx->msg->msg, temp_encrypted, CRYPTED_MSG_SIZE);
printf("temp_encrypted: \n %s \n ", temp_encrypted);
// printf("temp_encrypted: \n %s \n ", temp_encrypted);
// return crypto_box_ZEROBYTES + length - crypto_box_BOXZEROBYTES;
return 0;
}
......@@ -164,7 +165,10 @@ int pl_receive_message(struct pl_ctx * ctx)
//////////////////////////////////////////////////////////////
// pl_decrypt: actually decrypt the message with NaCl //
////////////////////////////////////////////////////////////
/*
* Decyrpts the message.
* FIXME is it not an idea to have this function return the decrypted message and not store it anywere?
*/
int pl_decrypt(struct pl_ctx * ctx, struct pl_pagermessage * msg, char plain[PLAIN_MSG_SIZE])
{
......@@ -187,7 +191,7 @@ int pl_decrypt(struct pl_ctx * ctx, struct pl_pagermessage * msg, char plain[PL
memcpy(plain, temp_plain + crypto_box_ZEROBYTES, PLAIN_MSG_SIZE);
// for now store unencrypted in place of encrypted, but should be stored encrypted no?
// 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;
......@@ -234,7 +238,7 @@ int pl_save_key(struct pl_keypair *key, char *filename)
#endif
}
struct pl_keypair * pl_load_key(char *filename)
int pl_load_key(struct pl_ctx *ctx, char *filename)
{
struct pl_keypair *keypair;
#ifdef ARDUINO
......@@ -245,7 +249,9 @@ struct pl_keypair * pl_load_key(char *filename)
lf = fopen(filename, "r");
fread(keypair, 1, sizeof(struct pl_keypair), lf);
fclose(lf);
return keypair;
memcpy(ctx->kp, keypair, sizeof(struct pl_keypair));
free(keypair);
return 0;
#endif
}
......@@ -263,9 +269,9 @@ int pl_save_public_key(char *pubkey, char *filename)
}
/*
* Load a public key from file and put it in the context so you can encrypt messages to it
* Load a public key from file, returns the publix key.
*/
int pl_load_public_key(struct pl_ctx *ctx, char *filename)
char * pl_load_public_key(char *filename)
{
char *public_key;
public_key = malloc(crypto_box_PUBLICKEYBYTES);
......@@ -273,8 +279,7 @@ int pl_load_public_key(struct pl_ctx *ctx, char *filename)
lf = fopen(filename, "r");
fread(public_key, 1, crypto_box_PUBLICKEYBYTES, lf);
fclose(lf);
memcpy(&ctx->receiver_public_key, public_key, sizeof(ctx->receiver_public_key));
return 0;
return public_key;
}
int pl_load_key_in_list(struct pl_ctx *ctx, struct pl_keypair *key)
......@@ -318,6 +323,50 @@ int pl_print_keylist(struct pl_ctx *ctx)
}
}
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key)
{
struct list_public_key *list, *ni, *last, *listprev;
list = ctx->public_keys;
ni = (struct list_public_key *) malloc( sizeof(struct list_public_key));
ni->id = compressed_point_to_addr(public_key);
ni->next = NULL;
memcpy(ni->public_key, public_key, crypto_box_PUBLICKEYBYTES);
if (list == NULL) {
ctx->public_keys = 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;
}
}
}
}
int pl_print_public_keylist(struct pl_ctx *ctx)
{
struct list_public_key * list;
#ifndef ARDUINO
printf("the list of loaded public keys: \n ");
#endif
for(list = ctx->public_keys; list != NULL; list = list->next) {
#ifndef ARDUINO
printf("address in list: %u \n", list->id );
#endif
}
return 0;
}
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg)
{
......@@ -329,7 +378,7 @@ int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg)
#endif
// make new list item
ni = (struct list_inbox *) malloc( sizeof(struct list_inbox) );
ni->id = ctx->msgcount;
ni->id = ctx->msgcount; // whut?
ni->next = NULL;
ni->status = MSG_NEW;
ni->msg = msg;
......
......@@ -10,20 +10,17 @@ extern "C"
#define ARDUINO
#endif
#include "../micro-ecc/uECC.h"
#include "things.h"
#ifdef ARDUINO
#include "../avrnacl/avrnacl.h"
#else
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#include <unistd.h>
#include <ctype.h>
#include <sodium.h>
#endif
#include "packets.h"
#include "things.h"
#ifdef DEBUG
#ifdef ARDUINO // fixme arduino debug functions
#define DBG
......@@ -40,47 +37,52 @@ extern "C"
#define DBM
#endif
#include "packets.h"
typedef enum { false, true } bool;
/*
* List of all the available keypairs
* Should we use same list for only pubkeys?
*/
typedef struct list_kp {
uint32_t id;
struct pl_keypair *kp;
struct list_kp *next;
// things maybe:
// - nickname
// - message counter
uint32_t id;
struct pl_keypair *kp;
struct list_kp *next;
// things maybe:
// - nickname
// - message counter
} list_kp;
/*
* List of all public keys, their addresses, names
*
*/
typedef struct list_public_key {
uint32_t id;
char public_key[crypto_box_PUBLICKEYBYTES];
bool is_group;
struct list_public_key *prev;
struct list_public_key *next;
} list_public_key;
/*
* 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;
int status; // 1 unread 2 read
int id;
struct pl_pagermessage *msg;
struct list_inbox *prev;
struct list_inbox *next;
} list_inbox;
/*
* pagerlib context for sending or receiving messages
*/
struct pl_ctx
{
// the curve we are using (maybe this should be somewhere else?)
// uECC_Curve curve;
// place to store the calculated shared secret
// char shared_secret[SHARED_SECRET_SIZE];
// place to store the decompressed public key
// char decompressed_point[64];
// contains the message that will be send or is received
char clear_message[MSG_SIZE];
char decryptbuffer[PLAIN_MSG_SIZE];
......@@ -90,44 +92,28 @@ struct pl_ctx
int msgcount;
struct list_inbox *inbox;
struct list_inbox *inbox_curr;
// uint8_t receiver_public_key[ECC_COMPRESSED_SIZE];
char receiver_public_key[crypto_box_PUBLICKEYBYTES];
struct list_public_key *public_keys;
uint8_t err;
//#ifdef ARDUINO
// aes_context aes_ctx;
//aes_context dec_aes;
//#else
//mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
//#endif
};
//const struct uECC_Curve_t * curve;
// mbedtls things
//mbedtls_aes_context enc_aes;
//mbedtls_aes_context dec_aes;
struct pl_ctx * pl_init();
struct pl_keypair * pl_create_keypair(struct pl_ctx*);
int pl_send_message(struct pl_ctx *);
int pl_set_receiver(struct pl_ctx *ctx, char *public_key);
int pl_send_message(struct pl_ctx *);
int pl_receive_message(struct pl_ctx*);
int pl_save_key(struct pl_keypair *key, char * filename);
struct pl_keypair * pl_load_key(char *filename);
int pl_load_key(struct pl_ctx *ctx, char *filename);
int pl_save_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);
char *pl_load_public_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);
int pl_load_public_key_in_list(struct pl_ctx *ctx, char *public_key);
int pl_print_public_keylist(struct pl_ctx *ctx);
int pl_inbox_append(struct pl_ctx *ctx, struct pl_pagermessage *msg);
uint8_t compressed_point_to_addr( uint8_t input[]);
int pl_inbox_display(struct pl_ctx *ctx);
int pl_inbox_next(struct pl_ctx *ctx);
......
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