Commit ee068ece authored by hark's avatar hark
Browse files

support for addresses so you only receive messages for you

parent 568fc8ba
CC=colorgcc
CFLAGS="-I ../libs"
CFLAGS="-I ../libs -DNODEBUG"
all: test rx-test tx-test keygen
......
......@@ -27,7 +27,7 @@ char decompressed_point[64];
int main() {
struct pl_keypair *receiver, *sender;
char clear_message[] = "Blaat blaat, dit is een test berichtje :) ";
char clear_message[] = "Blaat blaat, dit is een test berichtje :), en nog meer en meer en meer 123456744555 blablablablablablabal jajajajaj hee blaat ";
char crypt_message[128];
char decrypted_message[128];
// struct pl_pagermessage * sended_msg;
......@@ -49,8 +49,7 @@ int main() {
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(receiver, 2, sizeof(struct pl_keypair));
/* create keypairs */
/* create keypairs */
pl_create_keypair(context, sender);
pl_save_key(sender,"sender.keypair");
......@@ -61,34 +60,44 @@ int main() {
// context->kp = sender;
pl_load_key(sender, "sender.keypair");
pl_load_key(receiver, "receiver.keypair");
pl_load_key_in_list(context, sender);
pl_load_key_in_list(context, receiver);
pl_print_keylist(context);
context->kp = sender;
/*
DBG("\n sender.keypair");
DBM("sender", sizeof(struct pl_keypair), sender);
DBM("receiver", sizeof(struct pl_keypair), receiver);
DBM("context->kp (sender)", sizeof(struct pl_keypair), context->kp);
DBM("context->kp->private_key (sender)", sizeof(context->kp->private_key), &context->kp->private_key);
*/
memcpy(context->msg->msg, clear_message, MSG_SIZE);
DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
// DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
memcpy(&context->receiver_compressed_point, &receiver->compressed_point, sizeof(context->receiver_compressed_point));
DBM("context->receiver_compressed_point",sizeof(context->receiver_compressed_point) , &context->receiver_compressed_point);
// DBM("context->receiver_compressed_point",sizeof(context->receiver_compressed_point) , &context->receiver_compressed_point);
pl_send_message(context);
context->kp = receiver;
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);
// 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);
pl_receive_message(context);
if ( pl_receive_message(context) == 1) {
printf("failed to receive this message ! (exit 1)");
} else {
printf("the decrypted message: %s \n", 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);
......
......@@ -33,19 +33,10 @@ extern "C"
//#define AES_KEYSIZE 192
#define AES_KEYSIZE 256
#define IV_SIZE 16
/* message stuff
MSG_SIZE: size of the actual message
*/
#define MSG_SIZE 32
// pager.h
/*
* pager packet format
*/
// the text message
#define MSG_SIZE 80
// the address (hash or part of pubkey?)
#define ADDRESS_SIZE 2
struct pl_keypair
{
......@@ -60,26 +51,12 @@ struct pl_keypair
struct pl_pagermessage
{
uint32_t address;
uint8_t sender_compressed_point[ECC_COMPRESSED_SIZE];
unsigned char iv[IV_SIZE];
char msg[MSG_SIZE];
};
/* subjectPublicKeyInfo field
* in X.509 certificates [PKI]
* https://www.ietf.org/rfc/rfc5480.txt
* https://tools.ietf.org/html/rfc5915
*/
/*
* gpg
* A V4 fingerprint is the 160-bit SHA-1 hash of the octet 0x99,
* followed by the two-octet packet length, followed by the entire
* Public-Key packet starting with the version field. The Key ID is the
* low-order 64 bits of the fingerprint. Here are the fields of the
* hash material, with the example of a DSA key:
*/
/* lorawan
Rate Max payload size
......
......@@ -36,11 +36,27 @@ int rng (uint8_t *dest, unsigned size) {
close(fd);
return 1;
#else
// arduino rng comes here
return 0;
#endif
}
uint32_t compressed_point_to_addr( uint8_t input[])
{
uint32_t hashval;
int i = 0;
// DBM("hash address input", ECC_COMPRESSED_SIZE, input);
while( hashval < UINT32_MAX && i < ECC_COMPRESSED_SIZE ) {
// printf("hashval %u i: %u\n ", hashval, i);
hashval = hashval << 8;
hashval += input[ i ];
i++;
}
return hashval;
// return 123456;
}
inline struct pl_ctx * pl_init() {
struct pl_ctx * ctx;
......@@ -55,6 +71,9 @@ 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->keypairs = (struct list_kp *) malloc( sizeof(struct list_kp) );
uECC_set_rng(rng);
return ctx;
}
......@@ -87,10 +106,12 @@ inline int pl_send_message(struct pl_ctx *ctx) {
/* decompress key */
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);
// DBM("#receiver compressed point ",sizeof(ctx->receiver_compressed_point) , &ctx->receiver_compressed_point);
// DBM("#decompr point ",sizeof(ctx->decompressed_point) , &ctx->decompressed_point);
ctx->msg->address = compressed_point_to_addr(ctx->receiver_compressed_point);
DBM("address: ", sizeof(ctx->msg->address), &ctx->msg->address);
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)) {
......@@ -98,8 +119,8 @@ inline int pl_send_message(struct pl_ctx *ctx) {
return 1;
}
DBM("msg->msg in pl_send_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
DBM("shared secret in pl_send_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
// DBM("msg->msg in pl_send_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
// DBM("shared secret in pl_send_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
#ifdef ARDUINO
......@@ -118,12 +139,12 @@ inline int pl_send_message(struct pl_ctx *ctx) {
char civ[IV_SIZE];
memcpy(&civ, &ctx->msg->iv, IV_SIZE);
DBM("iv in pl_send_message before crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
// 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_free (&ctx->aes_ctx);
DBG("message to send: %s \n ", ctx->msg);
// DBG("message to send: %s \n ", ctx->msg);
#endif
#endif
......@@ -136,8 +157,8 @@ inline int pl_send_message(struct pl_ctx *ctx) {
base64_encode(&msg_base64, msg, sizeof(struct pl_pagermessage));
DBG(" \n encmsg: len: %u base64: \n %177.177s \n", sizeof(struct pl_pagermessage) ,msg_base64, msg);
*/
DBM("msg->msg in pl_send_message after crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
DBM("iv in pl_send_message after crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
// DBM("msg->msg in pl_send_message after crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
// DBM("iv in pl_send_message after crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
return 0;
}
......@@ -146,17 +167,33 @@ inline int pl_receive_message(struct pl_ctx * ctx)
memset(&ctx->shared_secret, 7, sizeof(ctx->shared_secret));
memset(&ctx->clear_message, 7, sizeof(ctx->clear_message));
DBM("msg->msg in pl_receive_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
// DBM("msg->msg in pl_receive_message before crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
/* 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;
}
}
// exit when address not found
if (found == 0) return 1;
/* decompress the senders public key */
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);
//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)) {
DBG("shared_secret() failed (receive)\n");
}
DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
// DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
#ifndef NOCRYPT
......@@ -171,7 +208,7 @@ inline int pl_receive_message(struct pl_ctx * ctx)
#else
/* decrypt the message */
mbedtls_aes_init( &ctx->aes_ctx );
DBM("iv in pl_receive_message", sizeof(ctx->msg->iv), &ctx->msg->iv);
//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 );
......@@ -235,6 +272,57 @@ pl_load_key(struct pl_keypair *key, char * filename) {
#endif
}
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->compressed_point);
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;
}
}
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) {
printf("address in list: %u \n", list->id );
/*
if (ctx->msg->address == list->id)
{
DBG("Found address in list!");
// set keypair to use
ctx->kp = list->kp;
}
else
{
DBG("address not found in list, message probably not for us");
return 1;
}
}
*/
}
}
......@@ -40,6 +40,13 @@ extern "C"
#endif
#include "packets.h"
typedef struct list_kp {
uint32_t id;
struct pl_keypair *kp;
struct list_kp *next;
} list_kp;
/*
* pagerlib context for sending or receiving messages
......@@ -54,8 +61,10 @@ struct pl_ctx
char decompressed_point[64];
// contains the message that will be send or is received
char clear_message[MSG_SIZE];
// my keypair
// my (currently used) keypair
struct pl_keypair *kp;
// keypair list (all available keypairs)
struct list_kp *keypairs;
// struct pl_keypair *to, *from;
struct pl_pagermessage *msg;
......@@ -88,6 +97,8 @@ 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);
uint32_t compressed_point_to_addr( uint8_t input[]);
#ifdef __cplusplus
} /* end of extern "C" */
#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