Commit cb23bb22 authored by hark's avatar hark

dingen

parent 9db7074f
Pipeline #40 skipped
......@@ -10,3 +10,6 @@
[submodule "libs/avr-crypto-lib"]
path = libs/avr-crypto-lib
url = https://github.com/cantora/avr-crypto-lib.git
[submodule "libs/avr_eefs"]
path = libs/avr_eefs
url = https://github.com/feilipu/avr_eefs.git
......@@ -6,7 +6,7 @@ all: test
base-tx: base-tx.c
$(CC) -o base-tx base-tx.c $(LIBS) $(CFLAGS)
test: test.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c
test: test.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c ../libs/pagerlib/pagerlib.h ../libs/pagerlib/packets.h
$(CC) -std=c99 -g -o test test.c ../libs/micro-ecc/uECC.c ../libs/pagerlib/pagerlib.c ../libs/pagerlib/things.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
test2: test2.c
$(CC) -std=c99 -o test2 test2.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
......@@ -23,20 +23,23 @@ char decompressed_point[64];
int main() {
struct pl_keypair *receiver;
struct pl_keypair *receiver, *sender;
char clear_message[] = "Blaat blaat, dit is een test berichtje :) ";
char crypt_message[128];
char decrypted_message[128];
struct pl_pagermessage * sended_msg;
// struct pl_pagermessage * sended_msg;
// initialise the pager
struct pl_ctx * context;
/*
if ( pl_init(context) < 1 )
{
DBG("pagerlib initialized \n");
}
*/
context = pl_init(context);
// sended_msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
sended_msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(sended_msg, 7, sizeof(struct pl_pagermessage));
// memset(sended_msg, 7, sizeof(struct pl_pagermessage));
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(sender, 9, sizeof(struct pl_keypair));
......@@ -45,24 +48,30 @@ int main() {
/* create keypairs */
pl_create_keypair(ctx->me);
pl_save_key(ctx->me,"sender.keypair");
pl_create_keypair(context, sender);
//pl_save_key(context->kp,"sender.keypair");
pl_create_keypair(sender);
pl_save_key(receiver,"receiver.keypair");
pl_create_keypair(context, receiver);
//pl_save_key(receiver,"receiver.keypair");
memcpy(sended_msg->msg, clear_message, MSG_SIZE);
//switch to sender key
context->kp = sender;
memcpy(context->msg->msg, clear_message, MSG_SIZE);
pl_send_message(receiver, sender, sended_msg, &shared_secret, &decompressed_point);
memcpy(context->receiver_compressed_point, receiver->compressed_point, sizeof(context->receiver_compressed_point));
//switch to receiver key
pl_send_message(context);
dump_buffer(sizeof(struct pl_pagermessage), sended_msg);
printf("\n The message that decrypted: \n");
pl_receive_message(receiver, sended_msg, &decrypted_message, &shared_secret, &decompressed_point);
printf("the message: %s", &decrypted_message);
printf("\n ----------------------------\n");
context->kp = receiver;
//printf("\n The message that decrypted: \n");
pl_receive_message(context);
printf("the message: %s", context->msg->msg);
// printf("\n ----------------------------\n");
return 0;
}
Subproject commit 502c2d88e6d0feebf7e846c6f7f7545648b04710
......@@ -17,10 +17,15 @@ extern "C"
uECC_secp256k1();
ECC_COMPRESSED_SIZE == SHARED_SECRET_SIZE FIXME
*/
#define CURVE uECC_secp192r1()
#define ECC_COMPRESSED_SIZE 24
#define SHARED_SECRET_SIZE 24
//#define ECC_COMPRESSED_SIZE 24
//#define SHARED_SECRET_SIZE 24
#define ECC_COMPRESSED_SIZE 33
#define SHARED_SECRET_SIZE 33
//#define CURVE uECC_secp192r1()
#define CURVE uECC_secp256k1()
/* message stuff
MSG_SIZE: size of the actual message
......@@ -36,28 +41,6 @@ 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
*/
......
......@@ -4,73 +4,47 @@
#include "packets.h"
#include <stdlib.h>
#include "things.h"
#include "pagerlib.h"
#define NOCRYPT
#if defined(__AVR_ATmega328P__)
#define ARDUINO
#endif
#ifdef ARDUINO
#include <AESLib.h>
#define DBG
#define DB
#else
#define DBG(f_, ...) printf((f_), ##__VA_ARGS__);
#define DB(f_, ...) dump_buffer((f_), ##__VA_ARGS__);
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
int rng (uint8_t *dest, unsigned size) {
#endif
//failed
return 0;
}
#ifdef ARDUINO
aes_context aes_ctx;
//aes_context dec_aes;
#else
mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
#endif
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
struct pl_ctx * pl_init() {
struct pl_ctx * ctx;
ctx = (struct pl_ctx *) malloc(sizeof(struct pl_ctx));
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));
*/
ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(ctx->kp, 9, sizeof(struct pl_keypair));
return 0;
#ifdef ARDUINO
uECC_set_rng(rng);
#endif
return ctx;
}
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
* Before calling this make sure u set sender ( ctx->kp , ctx->msg->sender_compressed_point) and receiver ctx->receiver_compressed_point, and the message you want to send
*/
int pl_send_message(struct pl_ctx *ctx) {
......@@ -83,15 +57,26 @@ int pl_send_message(struct pl_ctx *ctx) {
// copy compressed point into pager message
memcpy(ctx->msg->sender_compressed_point, ctx->kp->compressed_point, sizeof(ctx->msg->sender_compressed_point));
//DBG("Sending message \n");
#ifndef NOCRYPT
/* decompress key */
uECC_decompress(&ctx->decompressed_point, &ctx->receiver_compressed_point, ctx->curve);
/*calculate shared secret on sender*/
if (!uECC_shared_secret(ctx->msg->public_key, ctx->kp->private_key, ctx->shared_secret, ctx->curve)) {
if (!uECC_shared_secret(&ctx->decompressed_point, &ctx->kp->private_key, &ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
DBG("private key in pl_send_message \n");
dump_buffer(sizeof(ctx->kp->private_key), ctx->kp->private_key);
DBG("\n");
DBG("shared secret in pl_send_message \n");
dump_buffer(sizeof(ctx->shared_secret), ctx->shared_secret);
DBG("\n");
#ifdef ARDUINO
ctx->aes_ctx = aes192_cbc_enc_start(&ctx->shared_secret, ctx->msg->iv);
......@@ -111,7 +96,7 @@ int pl_send_message(struct pl_ctx *ctx) {
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);
// DBG("message to send: %s \n ", ctx->msg);
#endif
#endif
/*
......@@ -134,11 +119,22 @@ int pl_receive_message(struct pl_ctx * ctx)
uECC_decompress(ctx->msg->sender_compressed_point, ctx->decompressed_point, ctx->curve);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(ctx->decompressed_point, ctx->kp->private_key, ctx->shared_secret, ctx->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, ctx->shared_secret);
DB(IV_SIZE, ctx->msg->iv);
// DB(SHARED_SECRET_SIZE, ctx->shared_secret);
// DB(IV_SIZE, ctx->msg->iv);
DBG("private key in pl_receive_message \n");
dump_buffer(sizeof(ctx->kp->private_key), ctx->kp->private_key);
DBG("\n");
DBG("shared secret in pl_receive_message \n");
dump_buffer(sizeof(ctx->shared_secret), ctx->shared_secret);
DBG("\n");
#ifndef NOCRYPT
#ifdef ARDUINO
......@@ -151,22 +147,25 @@ int pl_receive_message(struct pl_ctx * ctx)
*/
#else
/* decrypt the message */
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_init( &ctx->aes_ctx );
mbedtls_aes_setkey_dec( &aes_ctx, ctx->shared_secret, 256 );
mbedtls_aes_setkey_dec( &ctx->aes_ctx, ctx->shared_secret, 256 );
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 );
mbedtls_aes_crypt_cbc( &ctx->aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, ctx->msg->iv, ctx->msg->msg, ctx->clear_message );
mbedtls_aes_free ( &ctx->aes_ctx );
#endif
#endif
DBG("decrypted_message: %s \n ", ctx->msg->msg);
DB(sizeof(ctx->clear_message), ctx->clear_message);
//DBG("decrypted_message: %s \n ", ctx->clear_message);
//DB(sizeof(ctx->clear_message), ctx->clear_message);
//printf("ctx: \n");
DB(sizeof(struct pl_ctx), ctx);
}
void pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
int pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
......@@ -180,12 +179,12 @@ void pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
}
uECC_compress(ctx->decompressed_point, keypair->compressed_point, ctx->curve);
DBG(" \n comp point \n ");
DB(sizeof(keypair->compressed_point), keypair->compressed_point);
DBG(" \n end comp point \n ");
//DBG(" \n comp point \n ");
//DB(sizeof(keypair->compressed_point), keypair->compressed_point);
//DBG(" \n end comp point \n ");
return keypair;
return 0;
}
int
......
......@@ -6,23 +6,76 @@ extern "C"
//#define NOCRYPT
#if defined(__AVR_ATmega328P__)
#define ARDUINO
#endif
#ifdef ARDUINO
#include <AESLib.h>
#define DBG
#define DB
#else
#define DBG(f_, ...) printf((f_), ##__VA_ARGS__);
#define DB(f_, ...) dump_buffer((f_), ##__VA_ARGS__);
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#endif
#include "packets.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 *kp;
// struct pl_keypair *to, *from;
struct pl_pagermessage *msg;
uint8_t receiver_compressed_point[ECC_COMPRESSED_SIZE];
#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;
void pl_init();
void pl_create_keypair(struct pl_keypair *);
void pl_send_message(struct pl_keypair *, struct pl_keypair *, struct pl_pagermessage *, char *, char *);
struct pl_ctx * pl_init();
int pl_create_keypair(struct pl_ctx*, struct pl_keypair *);
int pl_send_message(struct pl_ctx *);
void pl_receive_message(struct pl_keypair *, struct pl_pagermessage *, char *, char *, char *);
int pl_receive_message(struct pl_ctx*);
void pl_save_key(struct pl_keypair *key, char * filename);
void pl_load_key(struct pl_keypair *key, char * filename);
int pl_save_key(struct pl_keypair *key, char * filename);
int pl_load_key(struct pl_keypair *key, char * filename);
#ifdef __cplusplus
} /* end of extern "C" */
......
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