Commit 68dab2c9 authored by hark's avatar hark

update

parent 44cbbd48
Pipeline #34 skipped
......@@ -7,3 +7,6 @@
[submodule "libs/AESLib"]
path = libs/AESLib
url = https://github.com/y1ds/AESLib.git
[submodule "libs/avr-crypto-lib"]
path = libs/avr-crypto-lib
url = https://github.com/cantora/avr-crypto-lib.git
......@@ -17,33 +17,46 @@
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
char shared_secret[SHARED_SECRET_SIZE];
char decompressed_point[64];
int main() {
struct pl_keypair *sender, *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();
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));
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
memset(receiver, 2, sizeof(struct pl_keypair));
struct pl_pagermessage * sended_msg;
/* create keypairs */
sender = pl_create_keypair();
pl_create_keypair(sender);
pl_save_key(sender,"sender.keypair");
receiver = pl_create_keypair();
pl_create_keypair(sender);
pl_save_key(receiver,"receiver.keypair");
sended_msg = pl_send_message(receiver, sender, clear_message);
memcpy(sended_msg->msg, clear_message, MSG_SIZE);
pl_send_message(receiver, sender, sended_msg, &shared_secret, &decompressed_point);
printf("\n The message that was send: \n");
dump_buffer(sizeof(struct pl_pagermessage), sended_msg);
printf("\n ----------------------------\n");
printf("\n The message that decrypted: \n");
pl_receive_message(receiver, sended_msg);
pl_receive_message(receiver, sended_msg, &decrypted_message, &shared_secret, &decompressed_point);
printf("the message: %s", &decrypted_message);
printf("\n ----------------------------\n");
return 0;
......
Subproject commit b14938245d00969fde184d6c8490ff0d1a7ab7e1
#ifndef packets_h
#ifdef __cplusplus
extern "C"
{
#endif
#define ECC_COMPRESSED_SIZE 33
#define MSG_SIZE 128
#define SHARED_SECRET_SIZE 32
//256 bits aes
#define AES_KEYSIZE 256
#define IV_SIZE 16
// pager.h
......@@ -26,8 +38,8 @@ struct pl_keypair
struct pl_pagermessage
{
uint8_t sender_compressed_point[33];
char msg[128];
unsigned char iv[16];
char msg[MSG_SIZE];
unsigned char iv[IV_SIZE];
};
/* subjectPublicKeyInfo field
......@@ -63,4 +75,7 @@ Rate Max payload size
} /* end of extern "C" */
#endif
#endif
#define packets_h
......@@ -5,6 +5,8 @@
#include <stdlib.h>
#include "things.h"
#define NOCRYPT
const struct uECC_Curve_t * curve;
#if defined(__AVR_ATmega328P__)
......@@ -37,134 +39,119 @@ mbedtls_aes_context aes_ctx;
//mbedtls_aes_context dec_aes;
#endif
#define MSG_SIZE 128
#define SHARED_SECRET_SIZE 32
#define IV_SIZE 16
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
void pl_init() {
curve = uECC_secp256k1();
}
/*
msg = malloc(sizeof(struct pl_pagermessage));
memset(msg, 7, sizeof(struct pl_pagermessage));
keypair = malloc(sizeof(struct pl_keypair));
*/
struct pl_pagermessage * pl_send_message(struct pl_keypair *to, struct pl_keypair *from, char * msg_text) {
struct pl_pagermessage *msg;
}
char crypt_message[MSG_SIZE];
char decrypted_message[MSG_SIZE];
//unsigned char key[32];
unsigned char iv[IV_SIZE];
uint8_t shared_secret[SHARED_SECRET_SIZE] = {0};
pl_send_message(struct pl_keypair *to, struct pl_keypair *from, struct pl_pagermessage *msg, char * shared_secret, char * sender_decompressed_point) {
msg = malloc(sizeof(struct pl_pagermessage));
memset(msg, 7, sizeof(struct pl_pagermessage));
/* create a random iv */
// TODO (this is stupid)
memcpy(iv, fakeiv, sizeof(iv));
memcpy(msg->iv, fakeiv, sizeof(msg->iv));
// copy compressed point into pager message
memcpy(msg->sender_compressed_point, from->compressed_point, sizeof(msg->sender_compressed_point));
// copy iv for sending
memcpy(msg->iv, iv, sizeof(msg->iv));
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public_key, from->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
char tm[MSG_SIZE] = "dit is een test berichtje :) ";
#ifndef NOCRYPT
/*calculate shared secret on sender*/
if (!uECC_shared_secret(to->public_key, from->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
#ifdef ARDUINO
// copy the message into the output
memcpy(msg->msg, tm, MSG_SIZE);
aes_ctx = aes192_cbc_enc_start(shared_secret, iv);
aes192_cbc_enc_continue(aes_ctx, msg->msg, MSG_SIZE);
aes192_cbc_enc_finish(aes_ctx);
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);
#else
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_init( &aes_ctx );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &aes_ctx, shared_secret, 256 );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &aes_ctx, shared_secret, 256 );
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, MSG_SIZE, iv, tm, msg->msg );
mbedtls_aes_free (&aes_ctx);
#endif
DBG("message to send: %s \n ", msg_text);
char cmsg[MSG_SIZE];
memcpy(msg, msg->msg, sizeof(msg->iv));
char to_base64[sizeof(struct pl_keypair)];
base64_encode(&to_base64, to, sizeof(struct pl_keypair));
DBG(" \nto (keypair): len: %u base64: \n %129.129s \n", sizeof(struct pl_keypair) ,to_base64, to);
char msg_base64[sizeof(struct pl_pagermessage)];
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 msg;
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);
#endif
#endif
/*
char to_base64[sizeof(struct pl_keypair)];
base64_encode(&to_base64, to, sizeof(struct pl_keypair));
DBG(" \nto (keypair): len: %u base64: \n %129.129s \n", sizeof(struct pl_keypair) ,to_base64, to);
char msg_base64[sizeof(struct pl_pagermessage)];
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);
*/
}
void pl_receive_message(struct pl_keypair *me, struct pl_pagermessage *msg)
void pl_receive_message(struct pl_keypair *me, struct pl_pagermessage *msg, char * decrypted_message, char * shared_secret, char * sender_decompressed_point)
{
uint8_t sender_decompressed_point[64];
uint8_t shared_secret[SHARED_SECRET_SIZE] = {0};
char crypt_message[MSG_SIZE];
char decrypted_message[MSG_SIZE];
/* decompress the senders public key */
uECC_decompress(msg->sender_compressed_point, sender_decompressed_point, curve);
/* decompress the senders public key */
uECC_decompress(msg->sender_compressed_point, sender_decompressed_point, curve);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(sender_decompressed_point, me->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (receive)\n");
}
DB(SHARED_SECRET_SIZE, shared_secret);
DB(IV_SIZE, msg->iv);
/*calculate shared secret on receiver*/
if (!uECC_shared_secret(sender_decompressed_point, me->private_key, shared_secret, curve)) {
DBG("shared_secret() failed (receive)\n");
}
DB(SHARED_SECRET_SIZE, shared_secret);
DB(IV_SIZE, msg->iv);
#ifndef NOCRYPT
#ifdef ARDUINO
memcpy(decrypted_message, 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);
aes192_cbc_dec_finish(aes_ctx);
/*
aes_context aes_ctx;
aes_ctx = aes192_cbc_dec_start(shared_secret, msg->iv);
aes192_cbc_dec_continue(aes_ctx, decrypted_message, MSG_SIZE);
aes192_cbc_dec_finish(aes_ctx);
*/
#else
/* decrypt the message */
mbedtls_aes_init( &aes_ctx );
/* decrypt the message */
mbedtls_aes_init( &aes_ctx );
mbedtls_aes_setkey_dec( &aes_ctx, shared_secret, 256 );
mbedtls_aes_setkey_dec( &aes_ctx, shared_secret, 256 );
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, msg->iv, msg->msg, decrypted_message );
mbedtls_aes_free ( &aes_ctx );
mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, MSG_SIZE, msg->iv, msg->msg, decrypted_message );
mbedtls_aes_free ( &aes_ctx );
#endif
#endif
DBG("decrypted_message: %s \n ", decrypted_message);
DB(sizeof(decrypted_message), decrypted_message);
DBG("decrypted_message: %s \n ", msg->msg);
DB(sizeof(decrypted_message), decrypted_message);
}
struct pl_keypair * pl_create_keypair() {
struct pl_keypair *keypair;
keypair = malloc(sizeof(struct pl_keypair));
void pl_create_keypair(struct pl_keypair *keypair) {
memset(keypair->public_key, 3, sizeof(keypair->public_key));
memset(keypair->private_key, 4, sizeof(keypair->private_key));
memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
// memset(keypair->public_key, 3, sizeof(keypair->public_key));
// memset(keypair->private_key, 4, sizeof(keypair->private_key));
// memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
/* Generate arbitrary EC point (public) on Curve */
......@@ -172,7 +159,7 @@ struct pl_keypair * pl_create_keypair() {
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 ");
......
......@@ -15,14 +15,14 @@ extern "C"
//mbedtls_aes_context enc_aes;
//mbedtls_aes_context dec_aes;
void init();
struct keypair * pl_create_keypair();
struct pagermessage * pl_send_message(struct keypair *, struct keypair *, char *);
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 *);
void pl_receive_message(struct keypair *, struct pagermessage *);
void pl_receive_message(struct pl_keypair *, struct pl_pagermessage *, char *, char *, char *);
void pl_save_key(struct keypair *key, char * filename);
void pl_load_key(struct keypair *key, char * filename);
void pl_save_key(struct pl_keypair *key, char * filename);
void 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