Commit f4302b27 authored by hark's avatar hark
Browse files

Merge branch 'zooitje'

Conflicts:
	libs/pagerlib/packets.h
parents 5e86b49b 360f0f9c
......@@ -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,15 +23,23 @@ char decompressed_point[64];
int main() {
struct pl_keypair *sender, *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
pl_init();
sended_msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(sended_msg, 7, sizeof(struct pl_pagermessage));
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));
// memset(sended_msg, 7, sizeof(struct pl_pagermessage));
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(sender, 9, sizeof(struct pl_keypair));
......@@ -40,24 +48,43 @@ int main() {
/* create keypairs */
pl_create_keypair(sender);
/*
pl_create_keypair(context, sender);
pl_save_key(sender,"sender.keypair");
pl_create_keypair(sender);
pl_create_keypair(context, receiver);
pl_save_key(receiver,"receiver.keypair");
*/
//switch to sender key
// context->kp = sender;
pl_load_key(sender, "sender.keypair");
pl_load_key(receiver, "receiver.keypair");
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);
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);
memcpy(sended_msg->msg, clear_message, MSG_SIZE);
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);
pl_send_message(receiver, sender, sended_msg, &shared_secret, &decompressed_point);
dump_buffer(sizeof(struct pl_pagermessage), sended_msg);
pl_receive_message(context);
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");
printf("the decrypted message: %s \n", context->msg->msg);
// printf("\n ----------------------------\n");
return 0;
}
Subproject commit 502c2d88e6d0feebf7e846c6f7f7545648b04710
......@@ -17,23 +17,30 @@ extern "C"
uECC_secp256k1();
*/
/*
#define CURVE uECC_secp192r1()
#define ECC_COMPRESSED_SIZE 25
#define SHARED_SECRET_SIZE 24
/* message stuff
MSG_SIZE: size of the actual message
*/
#define MSG_SIZE 32
#define CURVE uECC_secp256r1()
#define ECC_COMPRESSED_SIZE 33
#define SHARED_SECRET_SIZE 39
/* AES stuff
AES_KEYSIZE: keysize of aes key
IV_SIZE: always 16? FIXME
*/
#define AES_KEYSIZE 192
//#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
/*
......@@ -43,7 +50,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];
};
......
......@@ -4,96 +4,123 @@
#include "packets.h"
#include <stdlib.h>
#include "things.h"
#include "pagerlib.h"
#define NOCRYPT
const struct uECC_Curve_t * curve;
#if defined(__AVR_ATmega328P__)
#define ARDUINO
#endif
#ifdef ARDUINO
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
#include <AESLib.h>
int rng (uint8_t *dest, unsigned size) {
#ifndef ARDUINO
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
if (fd == -1) {
return 0;
}
}
char *ptr = (char *)dest;
size_t left = size;
while (left > 0) {
size_t bytes_read = read(fd, ptr, left);
if (bytes_read <= 0) { // read failed
close(fd);
return 0;
}
left -= bytes_read;
ptr += bytes_read;
}
#define DBG
#define DB
close(fd);
return 1;
#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"
return 0;
#endif
}
#ifdef ARDUINO
aes_context aes_ctx;
//aes_context dec_aes;
#else
mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
#endif
inline struct pl_ctx * pl_init() {
struct pl_ctx * ctx;
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
ctx = (struct pl_ctx *) malloc(sizeof(struct pl_ctx));
ctx->curve = CURVE;
void pl_init() {
curve = CURVE;
/*
msg = malloc(sizeof(struct pl_pagermessage));
memset(msg, 7, sizeof(struct pl_pagermessage));
keypair = malloc(sizeof(struct pl_keypair));
*/
ctx->msg = (struct pl_pagermessage *) malloc(sizeof(struct pl_pagermessage));
memset(ctx->msg, 7, sizeof(struct pl_pagermessage));
ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(ctx->kp, 9, sizeof(struct pl_keypair));
uECC_set_rng(rng);
return ctx;
}
inline int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
{
return 1;
}
/*
* This sends a message (encrypts it)
* 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
*/
pl_send_message(struct pl_keypair *to, struct pl_keypair *from, struct pl_pagermessage *msg, char * shared_secret, char * sender_decompressed_point) {
inline int pl_send_message(struct pl_ctx *ctx) {
memset(ctx->shared_secret, 7, sizeof(ctx->shared_secret));
/* create a random iv */
// TODO (this is stupid)
memcpy(msg->iv, fakeiv, sizeof(msg->iv));
// memcpy(&ctx->msg->iv, &fakeiv, sizeof(ctx->msg->iv));
rng(&ctx->msg->iv, 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));
//DBG("Sending message \n");
#ifndef NOCRYPT
/* 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);
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public_key, from->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (1)\n");
if (!uECC_shared_secret(&ctx->decompressed_point, &ctx->kp->private_key, &ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed in send_message (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
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
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, AES_KEYSIZE );
char cmsg[MSG_SIZE];
memcpy(msg, msg->msg, sizeof(msg->iv));
memcpy(&cmsg, &ctx->msg->msg, MSG_SIZE);
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);
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, civ, &cmsg, &ctx->msg->msg );
mbedtls_aes_free (&ctx->aes_ctx);
// DBG("message to send: %s \n ", ctx->msg);
#endif
#endif
/*
......@@ -105,48 +132,61 @@ 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);
*/
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;
}
void pl_receive_message(struct pl_keypair *me, struct pl_pagermessage *msg, char * decrypted_message, char * shared_secret, char * sender_decompressed_point)
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);
/* 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);
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(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);
DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
#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_init( &ctx->aes_ctx );
DBM("iv in pl_receive_message", sizeof(ctx->msg->iv), &ctx->msg->iv);
mbedtls_aes_setkey_dec( &aes_ctx, shared_secret, 256 );
mbedtls_aes_setkey_dec( &ctx->aes_ctx, &ctx->shared_secret, AES_KEYSIZE );
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 );
memcpy(ctx->msg->msg, ctx->clear_message, MSG_SIZE);
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, msg->iv, msg->msg, decrypted_message );
mbedtls_aes_free ( &aes_ctx );
#endif
#endif
DBM("msg->msg in pl_receive_message after crypt", sizeof(ctx->msg->msg), &ctx->msg->msg);
DBM("iv in pl_receive_message after crypt", sizeof(ctx->msg->iv), &ctx->msg->iv);
DBG("decrypted_message: %s \n ", msg->msg);
DB(sizeof(decrypted_message), decrypted_message);
}
void pl_create_keypair(struct pl_keypair *keypair) {
inline int pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
......@@ -155,20 +195,16 @@ 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);
DBG(" \n comp point \n ");
DB(sizeof(keypair->compressed_point), keypair->compressed_point);
DBG(" \n end comp point \n ");
return keypair;
uECC_compress(&ctx->decompressed_point, &keypair->compressed_point, ctx->curve);
// DBG("compress failed in create_keypair");
return 0;
}
int
inline int
pl_save_key(struct pl_keypair *key, char * filename) {
#ifdef ARDUINO
......@@ -176,23 +212,24 @@ pl_save_key(struct pl_keypair *key, char * filename) {
return 0;
#else
FILE *f;
f = fopen(filename, "w");
fwrite(key, 1, sizeof(struct pl_keypair), f);
fclose(f);
FILE *sf;
sf = fopen(filename, "w");
fwrite(key, 1, sizeof(struct pl_keypair), sf);
fclose(sf);
#endif
}
int
inline int
pl_load_key(struct pl_keypair *key, char * filename) {
#ifdef ARDUINO
return 0;
#else
FILE *f;
f = fopen(filename, "r");
fread(key, 1, sizeof(struct pl_keypair), f);
fclose(f);
FILE *lf;
lf = fopen(filename, "r");
fread(key, 1, sizeof(struct pl_keypair), lf);
fclose(lf);
#endif
}
......
......@@ -6,23 +6,83 @@ extern "C"
//#define NOCRYPT
#if defined(__AVR_ATmega328P__)
#define ARDUINO
#endif
#include "../micro-ecc/uECC.h"
#ifdef ARDUINO
#include <AESLib.h>
#define DBG
#define DB
#define DBM
#else
#define DBG(f_, ...) printf((f_), ##__VA_ARGS__);
#define DB(f_, ...) dump_buffer((f_), ##__VA_ARGS__);
#define DBM(f_, ...) dump_buffer_msg((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?)
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];
// 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" */
......
......@@ -43,7 +43,6 @@ dump_buffer(unsigned int n, const unsigned char* buf)
{
const unsigned char *p, *end;
unsigned i, j;
printf("\n Buffer: \n ");
end = buf + n;
for (i = 0; ; i += 16) {
......@@ -66,6 +65,14 @@ dump_buffer(unsigned int n, const unsigned char* buf)
BREAKOUT:
return;
}
void
dump_buffer_msg(char * msg, unsigned int n, void * buf)
{
fprintf(stderr,"\n DBM: %s \n", msg);
dump_buffer(n, buf);
fprintf(stderr,"\n");
}
void vli_print(char *str, uint8_t *vli, unsigned int size) {
......
......@@ -4,11 +4,11 @@
cd arduino-tx
make clean && make -j4
sudo make do_upload
make do_upload
cd ../arduino-rx
make clean && make -j4
sudo make do_upload
make do_upload
cd ..
cd ../base-tx
cd base-tx
make test
cd ..
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