Commit ad42cbf0 authored by hark's avatar hark

pager lib

parent bb7b09be
Pipeline #29 skipped
[submodule "libs/micro-ecc"]
path = libs/micro-ecc
url = https://github.com/kmackay/micro-ecc.git
[submodule "arduino-build"]
path = arduino-build
url = ssh://git@gitlab.net.020wireless.nl:2222/yids/arduino-build.git
CC=colorgcc
#CFLAGS=-I ../include mbedtls/ -iquote ../libs/mbedtls-2.2.0/include/ -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'
#CFLAGS="-I ../include -iquote ../libs/mbedtls-2.2.0/include/mbedtls/ -DMBEDTLS_CONFIG_FILE='<mbedtls_config.h>'"
CFLAGS="-I ../include"
CFLAGS="-I ../libs"
#LIBS=../include/ecc.c ../include/oaes_base64.c ../include/oaes_lib.c ../include/rand.c ../libs/micro-ecc/uECC.h
#LIBS=
all: test
base-tx: base-tx.c
$(CC) -o base-tx base-tx.c $(LIBS) $(CFLAGS)
test: test.c common.c
$(CC) -std=c99 -g -o test test.c common.c ../libs/micro-ecc/uECC.c -lmbedtls -lmbedx509 -lmbedcrypto $(LIBS) $(CFLAGS)
test: test.c ../libs/pagerlib/pagerlib.c
$(CC) -std=c99 -g -o test test.c ../libs/micro-ecc/uECC.c ../libs/pagerlib/pagerlib.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)
......@@ -6,182 +6,17 @@
//#include "../libs/mbedtls-2.2.0/include/mbedtls/config.h"
//#include "../libs/mbedtls-2.2.0/include/mbedtls/aes.h"
//#include "../libs/mbedtls-2.2.0/library/aes.c"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
//#include <gnutls/gnutls.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "packets.h"
#include "common.h"
//#include "packets.h"
#include "../libs/pagerlib/pagerlib.h"
#ifndef uECC_TEST_NUMBER_OF_ITERATIONS
#define uECC_TEST_NUMBER_OF_ITERATIONS 1
#endif
#define MSG_SIZE 128
// mbedtls things
mbedtls_aes_context enc_aes;
mbedtls_aes_context dec_aes;
const struct uECC_Curve_t * curve;
void init() {
curve = uECC_secp256k1();
}
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
struct pagermessage * send_message(struct keypair *to, struct keypair *from, char * msg_text) {
struct pagermessage *msg;
char crypt_message[128];
char decrypted_message[128];
//unsigned char key[32];
unsigned char iv[16];
uint8_t shared_secret[32] = {0};
msg = malloc(sizeof(struct pagermessage));
memset(msg, 7, sizeof(struct pagermessage));
/* create a random iv */
// TODO (this is stupid)
memcpy(iv, fakeiv, sizeof(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, from->private, shared_secret, curve)) {
printf("shared_secret() failed (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
mbedtls_aes_init( &enc_aes );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &enc_aes, shared_secret, 256 );
char tm[128] = "dit is een test berichtje :) ";
mbedtls_aes_crypt_cbc( &enc_aes, MBEDTLS_AES_ENCRYPT, MSG_SIZE, iv, tm, msg->msg );
printf("message to send: %s \n ", msg_text);
/*
printf("sender_shared_secrete: \n ");
dump_buffer(sizeof(sender_shared_secret), sender_shared_secret);
printf("clear_message: \n ");
dump_buffer(sizeof(clear_message), clear_message);
printf("crypt_message: \n ");
char bs_dec[128];
char bs_msg[128];
memset(bs_dec, 0, 128);
memset(bs_msg, 0, 128);
base64_encode(&bs_msg, &clear_message, 128 );
base64_decode(bs_dec, bs_msg, 128 );
printf(" \n base64: %128.128s \n dec: %s \n", bs_msg, bs_dec);
dump_buffer(sizeof(crypt_message), crypt_message);
*/
// dump_buffer(sizeof(struct pagermessage), msg);
char to_base64[sizeof(struct keypair)];
base64_encode(&to_base64, to, sizeof(struct keypair));
printf(" \nto (keypair): len: %u base64: \n %129.129s \n", sizeof(struct keypair) ,to_base64, to);
char msg_base64[sizeof(struct pagermessage)];
base64_encode(&msg_base64, msg, sizeof(struct pagermessage));
printf(" \n encmsg: len: %u base64: \n %177.177s \n", sizeof(struct pagermessage) ,msg_base64, msg);
return msg;
}
void receive_message(struct keypair *me, struct pagermessage *msg)
{
uint8_t sender_decompressed_point[64];
uint8_t shared_secret[32] = {0};
char crypt_message[128];
char decrypted_message[128];
/* 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, shared_secret, curve)) {
printf("shared_secret() failed (receive)\n");
}
dump_buffer(32, shared_secret);
dump_buffer(16, msg->iv);
/* decrypt the message */
mbedtls_aes_init( &dec_aes );
mbedtls_aes_setkey_dec( &dec_aes, shared_secret, 256 );
mbedtls_aes_crypt_cbc( &dec_aes, MBEDTLS_AES_DECRYPT, 128, msg->iv, msg->msg, decrypted_message );
printf("decrypted_message: %s \n ", decrypted_message);
dump_buffer(sizeof(decrypted_message), decrypted_message);
}
struct keypair * create_keypair() {
struct keypair *keypair;
keypair = malloc(sizeof(struct keypair));
memset(keypair->public, 3, sizeof(keypair->public));
memset(keypair->private, 4, sizeof(keypair->private));
memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(keypair->public, keypair->private, curve)) {
printf("uECC_make_key() failed\n");
}
uECC_compress(keypair->public, keypair->compressed_point, curve);
printf(" \n comp point \n ");
dump_buffer(32, keypair->compressed_point);
printf(" \n end comp point \n ");
return keypair;
}
save_key(struct keypair *key, char * filename) {
FILE *f;
f = fopen(filename, "w");
fwrite(key, 1, sizeof(struct keypair), f);
fclose(f);
}
load_key(struct keypair *key, char * filename) {
FILE *f;
f = fopen(filename, "r");
fread(key, 1, sizeof(struct keypair), f);
fclose(f);
}
void pb(char buf) {
}
int main() {
struct keypair *sender, *receiver;
......
// pager.h
/*
* pager packet format
*/
......@@ -49,3 +52,5 @@ Rate Max payload size
7 222
8:15 not defined
*/
#include <stdio.h>
#include <string.h>
#include "../libs/micro-ecc/uECC.h"
#include "packets.h"
#include "../micro-ecc/uECC.h"
#include "packets.h"
#include <stdlib.h>
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#ifdef AVR
......@@ -191,4 +193,165 @@ int base64_dec_len(char * input, int inputLen) {
return ((6 * inputLen) / 8) - numEq;
}
////////////////////////////////////////////////
const struct uECC_Curve_t * curve;
mbedtls_aes_context enc_aes;
mbedtls_aes_context dec_aes;
#define MSG_SIZE 128
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
void init() {
curve = uECC_secp256k1();
}
struct pagermessage * send_message(struct keypair *to, struct keypair *from, char * msg_text) {
struct pagermessage *msg;
char crypt_message[128];
char decrypted_message[128];
//unsigned char key[32];
unsigned char iv[16];
uint8_t shared_secret[32] = {0};
msg = malloc(sizeof(struct pagermessage));
memset(msg, 7, sizeof(struct pagermessage));
/* create a random iv */
// TODO (this is stupid)
memcpy(iv, fakeiv, sizeof(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, from->private, shared_secret, curve)) {
printf("shared_secret() failed (1)\n");
return 1;
}
// dump_buffer(32, shared_secret);
// dump_buffer(16, msg->iv);
mbedtls_aes_init( &enc_aes );
/* encrypt message with aes using shared secret as the key */
mbedtls_aes_setkey_enc( &enc_aes, shared_secret, 256 );
char tm[128] = "dit is een test berichtje :) ";
mbedtls_aes_crypt_cbc( &enc_aes, MBEDTLS_AES_ENCRYPT, MSG_SIZE, iv, tm, msg->msg );
printf("message to send: %s \n ", msg_text);
/*
printf("sender_shared_secrete: \n ");
dump_buffer(sizeof(sender_shared_secret), sender_shared_secret);
printf("clear_message: \n ");
dump_buffer(sizeof(clear_message), clear_message);
printf("crypt_message: \n ");
char bs_dec[128];
char bs_msg[128];
memset(bs_dec, 0, 128);
memset(bs_msg, 0, 128);
base64_encode(&bs_msg, &clear_message, 128 );
base64_decode(bs_dec, bs_msg, 128 );
printf(" \n base64: %128.128s \n dec: %s \n", bs_msg, bs_dec);
dump_buffer(sizeof(crypt_message), crypt_message);
*/
// dump_buffer(sizeof(struct pagermessage), msg);
char to_base64[sizeof(struct keypair)];
base64_encode(&to_base64, to, sizeof(struct keypair));
printf(" \nto (keypair): len: %u base64: \n %129.129s \n", sizeof(struct keypair) ,to_base64, to);
char msg_base64[sizeof(struct pagermessage)];
base64_encode(&msg_base64, msg, sizeof(struct pagermessage));
printf(" \n encmsg: len: %u base64: \n %177.177s \n", sizeof(struct pagermessage) ,msg_base64, msg);
return msg;
}
void receive_message(struct keypair *me, struct pagermessage *msg)
{
uint8_t sender_decompressed_point[64];
uint8_t shared_secret[32] = {0};
char crypt_message[128];
char decrypted_message[128];
/* 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, shared_secret, curve)) {
printf("shared_secret() failed (receive)\n");
}
dump_buffer(32, shared_secret);
dump_buffer(16, msg->iv);
/* decrypt the message */
mbedtls_aes_init( &dec_aes );
mbedtls_aes_setkey_dec( &dec_aes, shared_secret, 256 );
mbedtls_aes_crypt_cbc( &dec_aes, MBEDTLS_AES_DECRYPT, 128, msg->iv, msg->msg, decrypted_message );
printf("decrypted_message: %s \n ", decrypted_message);
dump_buffer(sizeof(decrypted_message), decrypted_message);
}
struct keypair * create_keypair() {
struct keypair *keypair;
keypair = malloc(sizeof(struct keypair));
memset(keypair->public, 3, sizeof(keypair->public));
memset(keypair->private, 4, sizeof(keypair->private));
memset(keypair->compressed_point, 5, sizeof(keypair->compressed_point));
/* Generate arbitrary EC point (public) on Curve */
if (!uECC_make_key(keypair->public, keypair->private, curve)) {
printf("uECC_make_key() failed\n");
}
uECC_compress(keypair->public, keypair->compressed_point, curve);
printf(" \n comp point \n ");
dump_buffer(32, keypair->compressed_point);
printf(" \n end comp point \n ");
return keypair;
}
save_key(struct keypair *key, char * filename) {
FILE *f;
f = fopen(filename, "w");
fwrite(key, 1, sizeof(struct keypair), f);
fclose(f);
}
load_key(struct keypair *key, char * filename) {
FILE *f;
f = fopen(filename, "r");
fread(key, 1, sizeof(struct keypair), f);
fclose(f);
}
#include "packets.h"
const struct uECC_Curve_t * curve;
// mbedtls things
//mbedtls_aes_context enc_aes;
//mbedtls_aes_context dec_aes;
void init();
struct pagermessage * send_message(struct keypair *, struct keypair *, char *);
void receive_message(struct keypair *me, struct pagermessage *msg);
void receive_message(struct keypair *, struct pagermessage *);
struct keypair * create_keypair();
void save_key(struct keypair *key, char * filename);
void load_key(struct keypair *key, char * filename);
// common.h
void dump_buffer(unsigned int , const unsigned char* );
void vli_print(char *, uint8_t *, unsigned int);
......
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