Commit 9db7074f authored by hark's avatar hark

redid pagerlib

parent 226c96da
Pipeline #37 skipped
......@@ -23,13 +23,18 @@ char decompressed_point[64];
int main() {
struct pl_keypair *sender, *receiver;
struct pl_keypair *receiver;
char clear_message[] = "Blaat blaat, dit is een test berichtje :) ";
char crypt_message[128];
char decrypted_message[128];
struct pl_pagermessage * sended_msg;
// initialise the pager
pl_init();
struct pl_ctx * context;
if ( pl_init(context) < 1 )
{
DBG("pagerlib initialized \n");
}
sended_msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(sended_msg, 7, sizeof(struct pl_pagermessage));
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
......@@ -40,8 +45,8 @@ int main() {
/* create keypairs */
pl_create_keypair(sender);
pl_save_key(sender,"sender.keypair");
pl_create_keypair(ctx->me);
pl_save_key(ctx->me,"sender.keypair");
pl_create_keypair(sender);
pl_save_key(receiver,"receiver.keypair");
......
......@@ -36,6 +36,28 @@ extern "C"
// pager.h
/*
* pagerlib context for sending or receiving messages
*/
struct pl_ctx
{
// the curve we are using (maybe this should be somewhere else?)
const struct uECC_Curve_t * 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];
// my keypair
struct pl_keypair *me;
// struct pl_keypair *to, *from;
struct pl_pagermessage *msg;
receiver_compressed_point[ECC_COMPRESSED_SIZE];
};
/*
* pager packet format
*/
......@@ -43,7 +65,7 @@ extern "C"
struct pl_keypair
{
uint8_t public_key[64];
// uint8_t public_key[64];
uint8_t private_key[32];
uint8_t compressed_point[ECC_COMPRESSED_SIZE];
};
......
......@@ -7,7 +7,6 @@
#define NOCRYPT
const struct uECC_Curve_t * curve;
#if defined(__AVR_ATmega328P__)
#define ARDUINO
......@@ -42,33 +41,52 @@ mbedtls_aes_context aes_ctx;
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
void pl_init() {
curve = CURVE;
int pl_init(struct pl_ctx * ctx ) {
ctx->curve = CURVE;
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(ctx->msg, 7, sizeof(struct pl_pagermessage));
ctx->me = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(ctx->me, 9, sizeof(struct pl_keypair));
//ctx->receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
//memset(ctx->receiver, 2, sizeof(struct pl_keypair));
/*
msg = malloc(sizeof(struct pl_pagermessage));
memset(msg, 7, sizeof(struct pl_pagermessage));
keypair = malloc(sizeof(struct pl_keypair));
*/
return 0;
}
int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
{
return 0;
}
/*
* This sends a message (encrypts it)
* Before calling this make sure u set sender ( ctx->me , ctx->msg->sender_compressed_point) and receiver ctx->receiver_compressed_point, and the message you want to send
*/
pl_send_message(struct pl_keypair *to, struct pl_keypair *from, struct pl_pagermessage *msg, char * shared_secret, char * sender_decompressed_point) {
int pl_send_message(struct pl_ctx *ctx) {
/* create a random iv */
// TODO (this is stupid)
memcpy(msg->iv, fakeiv, sizeof(msg->iv));
memcpy(ctx->msg->iv, fakeiv, sizeof(ctx->msg->iv));
// copy compressed point into pager message
memcpy(msg->sender_compressed_point, from->compressed_point, sizeof(msg->sender_compressed_point));
memcpy(ctx->msg->sender_compressed_point, ctx->kp->compressed_point, sizeof(ctx->msg->sender_compressed_point));
#ifndef NOCRYPT
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public_key, from->private_key, shared_secret, curve)) {
if (!uECC_shared_secret(ctx->msg->public_key, ctx->kp->private_key, ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed (1)\n");
return 1;
}
......@@ -76,24 +94,24 @@ pl_send_message(struct pl_keypair *to, struct pl_keypair *from, struct pl_pager
// dump_buffer(16, msg->iv);
#ifdef ARDUINO
aes_ctx = aes192_cbc_enc_start(&shared_secret, msg->iv);
aes192_cbc_enc_continue(aes_ctx, msg->msg, MSG_SIZE);
aes192_cbc_enc_finish(aes_ctx);
ctx->aes_ctx = aes192_cbc_enc_start(&ctx->shared_secret, ctx->msg->iv);
aes192_cbc_enc_continue(ctx->aes_ctx, ctx->msg->msg, MSG_SIZE);
aes192_cbc_enc_finish(ctx->aes_ctx);
#else
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_init( &ctx->aes_ctx );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &aes_ctx, shared_secret, 256 );
mbedtls_aes_setkey_enc( &ctx->aes_ctx, ctx->shared_secret, 256 );
char cmsg[MSG_SIZE];
memcpy(msg, msg->msg, sizeof(msg->iv));
memcpy(cmsg, ctx->msg->msg, sizeof(ctx->msg->iv));
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, msg->iv, cmsg, msg->msg );
mbedtls_aes_free (&aes_ctx);
DBG("message to send: %s \n ", msg);
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, ctx->msg->iv, cmsg, ctx->msg->msg );
mbedtls_aes_free (&ctx->aes_ctx);
DBG("message to send: %s \n ", ctx->msg);
#endif
#endif
/*
......@@ -105,48 +123,50 @@ pl_send_message(struct pl_keypair *to, struct pl_keypair *from, struct pl_pager
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);
*/
return 0;
}
void pl_receive_message(struct pl_keypair *me, struct pl_pagermessage *msg, char * decrypted_message, char * shared_secret, char * sender_decompressed_point)
int pl_receive_message(struct pl_ctx * ctx)
{
/* decompress the senders public key */
uECC_decompress(msg->sender_compressed_point, sender_decompressed_point, curve);
uECC_decompress(ctx->msg->sender_compressed_point, ctx->decompressed_point, ctx->curve);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(sender_decompressed_point, me->private_key, shared_secret, curve)) {
if (!uECC_shared_secret(ctx->decompressed_point, ctx->kp->private_key, ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed (receive)\n");
}
DB(SHARED_SECRET_SIZE, shared_secret);
DB(IV_SIZE, msg->iv);
DB(SHARED_SECRET_SIZE, ctx->shared_secret);
DB(IV_SIZE, ctx->msg->iv);
#ifndef NOCRYPT
#ifdef ARDUINO
memcpy(decrypted_message, msg->msg, MSG_SIZE);
memcpy(ctx->clear_message, ctx->msg->msg, MSG_SIZE);
/*
aes_context aes_ctx;
aes_ctx = aes192_cbc_dec_start(shared_secret, msg->iv);
aes192_cbc_dec_continue(aes_ctx, decrypted_message, MSG_SIZE);
aes_ctx = aes192_cbc_dec_start(ctx->shared_secret, ctx->msg->iv);
aes192_cbc_dec_continue(aes_ctx, ctx->clear_message, MSG_SIZE);
aes192_cbc_dec_finish(aes_ctx);
*/
#else
/* decrypt the message */
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_setkey_dec( &aes_ctx, shared_secret, 256 );
mbedtls_aes_setkey_dec( &aes_ctx, ctx->shared_secret, 256 );
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, msg->iv, msg->msg, decrypted_message );
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, ctx->msg->iv, ctx->msg->msg, ctx->clear_message );
mbedtls_aes_free ( &aes_ctx );
#endif
#endif
DBG("decrypted_message: %s \n ", msg->msg);
DB(sizeof(decrypted_message), decrypted_message);
DBG("decrypted_message: %s \n ", ctx->msg->msg);
DB(sizeof(ctx->clear_message), ctx->clear_message);
}
void pl_create_keypair(struct pl_keypair *keypair) {
void pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
......@@ -155,10 +175,10 @@ void pl_create_keypair(struct pl_keypair *keypair) {
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(keypair->public_key, keypair->private_key, curve)) {
if (!uECC_make_key(ctx->decompressed_point, keypair->private_key, ctx->curve)) {
DBG("uECC_make_key() failed\n");
}
uECC_compress(keypair->public_key, keypair->compressed_point, curve);
uECC_compress(ctx->decompressed_point, keypair->compressed_point, ctx->curve);
DBG(" \n comp point \n ");
DB(sizeof(keypair->compressed_point), keypair->compressed_point);
......
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