Commit 360f0f9c authored by hark's avatar hark

works now

parent cb23bb22
......@@ -48,29 +48,42 @@ int main() {
/* create keypairs */
/*
pl_create_keypair(context, sender);
//pl_save_key(context->kp,"sender.keypair");
pl_save_key(sender,"sender.keypair");
pl_create_keypair(context, receiver);
//pl_save_key(receiver,"receiver.keypair");
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);
memcpy(context->receiver_compressed_point, receiver->compressed_point, sizeof(context->receiver_compressed_point));
DBM("receiver->compressed",sizeof(receiver->compressed_point) , &receiver->compressed_point);
//switch to receiver key
pl_send_message(context);
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);
pl_send_message(context);
context->kp = receiver;
//printf("\n The message that decrypted: \n");
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);
printf("the message: %s", context->msg->msg);
printf("the decrypted message: %s \n", context->msg->msg);
// printf("\n ----------------------------\n");
return 0;
......
......@@ -17,28 +17,29 @@ 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 CURVE uECC_secp256r1()
#define ECC_COMPRESSED_SIZE 33
#define SHARED_SECRET_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 256
#define IV_SIZE 16
//#define CURVE uECC_secp192r1()
#define CURVE uECC_secp256k1()
/* message stuff
MSG_SIZE: size of the actual message
*/
#define MSG_SIZE 32
/* AES stuff
AES_KEYSIZE: keysize of aes key
IV_SIZE: always 16? FIXME
*/
#define AES_KEYSIZE 192
#define IV_SIZE 16
// pager.h
/*
......
......@@ -7,17 +7,39 @@
#include "pagerlib.h"
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
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;
}
//failed
close(fd);
return 1;
#else
return 0;
#endif
}
struct pl_ctx * pl_init() {
inline struct pl_ctx * pl_init() {
struct pl_ctx * ctx;
ctx = (struct pl_ctx *) malloc(sizeof(struct pl_ctx));
......@@ -30,16 +52,14 @@ struct pl_ctx * pl_init() {
ctx->kp = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(ctx->kp, 9, sizeof(struct pl_keypair));
#ifdef ARDUINO
uECC_set_rng(rng);
#endif
return ctx;
}
int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
inline int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
{
return 0;
return 1;
}
/*
......@@ -47,35 +67,36 @@ int pl_set_receiver(struct pl_ctx *ctx, struct pl_keypair *keypair)
* 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) {
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(ctx->msg->iv, fakeiv, sizeof(ctx->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(ctx->msg->sender_compressed_point, ctx->kp->compressed_point, sizeof(ctx->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->decompressed_point, &ctx->receiver_compressed_point, ctx->curve);
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(&ctx->decompressed_point, &ctx->kp->private_key, &ctx->shared_secret, ctx->curve)) {
DBG("shared_secret() failed (1)\n");
DBG("shared_secret() failed in send_message (1)\n");
return 1;
}
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");
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
......@@ -83,18 +104,21 @@ int pl_send_message(struct pl_ctx *ctx) {
aes192_cbc_enc_continue(ctx->aes_ctx, ctx->msg->msg, MSG_SIZE);
aes192_cbc_enc_finish(ctx->aes_ctx);
#else
mbedtls_aes_init( &ctx->aes_ctx );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &ctx->aes_ctx, ctx->shared_secret, 256 );
mbedtls_aes_setkey_enc( &ctx->aes_ctx, &ctx->shared_secret, AES_KEYSIZE );
char cmsg[MSG_SIZE];
memcpy(cmsg, ctx->msg->msg, sizeof(ctx->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( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, ctx->msg->iv, cmsg, ctx->msg->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
......@@ -108,32 +132,29 @@ 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);
return 0;
}
int pl_receive_message(struct pl_ctx * ctx)
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(ctx->msg->sender_compressed_point, ctx->decompressed_point, ctx->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(&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);
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");
DBM("shared secret in pl_receive_message", sizeof(ctx->shared_secret), &ctx->shared_secret);
#ifndef NOCRYPT
......@@ -148,24 +169,24 @@ 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);
mbedtls_aes_setkey_dec( &ctx->aes_ctx, 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_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);
#endif
#endif
//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);
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);
}
int pl_create_keypair(struct pl_ctx *ctx, 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));
......@@ -174,20 +195,16 @@ int pl_create_keypair(struct pl_ctx *ctx, struct pl_keypair *keypair) {
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(ctx->decompressed_point, keypair->private_key, ctx->curve)) {
if (!uECC_make_key(&ctx->decompressed_point, &keypair->private_key, ctx->curve)) {
DBG("uECC_make_key() failed\n");
}
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 ");
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
......@@ -195,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
}
......
......@@ -13,13 +13,20 @@ extern "C"
#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"
......@@ -36,7 +43,7 @@ extern "C"
struct pl_ctx
{
// the curve we are using (maybe this should be somewhere else?)
const struct uECC_Curve_t * curve;
uECC_Curve curve;
// place to store the calculated shared secret
char shared_secret[SHARED_SECRET_SIZE];
// place to store the decompressed public key
......@@ -54,7 +61,7 @@ struct pl_ctx
aes_context aes_ctx;
//aes_context dec_aes;
#else
mbedtls_aes_context aes_ctx;
mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
#endif
......
......@@ -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) {
......
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