Commit 92f42620 authored by hark's avatar hark

Merge branch 'master' of 185.52.224.4:yids/avr-pager

Conflicts:
	base-tx/test.c
parents cea680fd a65b911e
......@@ -21,138 +21,30 @@ aes_context ctx; // context for the cbc crypto stuff
#define MESSAGE_SIZE 32
#define PAGER_MESSAGE_SIZE 57
//#define MESSAGE_SIZE 64
//#define PAGER_MESSAGE_SIZE 99
RH_ASK driver(2000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
uint8_t sharedSecret[NUM_ECC_DIGITS];
// uECC keys 192bit
uint8_t privkey[NUM_ECC_DIGITS+1] = {0xAD, 0x98, 0x8E, 0xC4, 0x79, 0x1D, 0xE0, 0x2C, 0xEE, 0xF8, 0xB0, 0xAA, 0xC9, 0x3E, 0x6F, 0x9D, 0x1E, 0x5E, 0xF7, 0x96, 0xD7, 0x3F, 0x7F, 0x2E, 0xF4};
static int RNG(uint8_t *dest, unsigned size) {
// Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of
// random noise). This can take a long time to generate random data if the result of analogRead(0)
// doesn't change very frequently.
while (size) {
uint8_t val = 0;
for (unsigned i = 0; i < 8; ++i) {
int init = analogRead(0);
int count = 0;
while (analogRead(0) == init) {
++count;
}
if (count == 0) {
val = (val << 1) | (init & 0x01);
} else {
val = (val << 1) | (count & 0x01);
}
}
*dest = val;
++dest;
--size;
}
// NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar.
return 1;
}
void generateKeys()
{
Serial.println("generating keys");
const struct uECC_Curve_t * curve = CURVE;
uint8_t private1[NUM_ECC_DIGITS+1];
uint8_t public1[NUM_ECC_DIGITS*2];
uint8_t secret1[NUM_ECC_DIGITS];
unsigned long a = millis();
uECC_make_key(public1, private1, curve);
unsigned long b = millis();
Serial.print("Generated keypair in "); Serial.print(b-a); Serial.println("Milliseconds");
Serial.print("uint8_t pubkey[NUM_ECC_DIGITS*2] = {");
for( int i = 0 ; i < sizeof(public1); i++){
Serial.print("0x");
Serial.print(public1[i], HEX);
Serial.print(", ");
}
Serial.println("}");
Serial.print("uint8_t privkey[NUM_ECC_DIGITS+1] = {");
for( int i = 0 ; i < sizeof(private1); i++){
Serial.print("0x");
Serial.print(private1[i], HEX);
Serial.print(", ");
}
Serial.println("}");
}
void hashSecret(uint8_t *p_secret)
{
Serial.println("Secret:");
for( int i=0; i < NUM_ECC_DIGITS; i++){
Serial.print(p_secret[i]);
Serial.print(" ");
}
Serial.println("");
}
char * encryptAES(char* p_data, uint8_t *p_key)
{
uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
aes_context ctx;
ctx = aes192_cbc_enc_start(p_key, iv);
aes192_cbc_enc_continue(ctx, p_data, 32);
aes192_cbc_enc_finish(ctx);
// Serial.print("encrypted-cbc:");
// Serial.println(p_data);
return p_data;
}
uint8_t tx_priv_key[ECC_COMPRESSED_SIZE-1] = {0xB4, 0x15, 0x70, 0x3E, 0xA7, 0x5D, 0x06, 0xF2, 0x33, 0x75, 0x8E, 0xE0, 0x86, 0x1B, 0x73, 0xBE, 0xEC, 0x87, 0x36, 0x0F, 0xF5, 0xE1, 0x79, 0x76, 0x2C, 0x3C, 0x74, 0x69, 0x83, 0x71, 0x07, 0xB5};
uint8_t tx_comp_p[ECC_COMPRESSED_SIZE] = {0x02, 0xA3, 0x91, 0x16, 0x71, 0x1E, 0x7B, 0xB2, 0x51, 0x7F, 0xD4, 0xF4, 0xC7, 0x81, 0x49, 0x75, 0x8B, 0x48, 0x75, 0x59, 0x27, 0xC9, 0x94, 0x3F, 0x59, 0xDD, 0xFF, 0x2E, 0x89, 0xE9, 0xD8, 0x1D, 0x2B };
char* decryptAES(char* p_data, uint8_t *p_key)
{
// Serial.println("Decrypting with key:");
// hashSecret(sharedSecret);
uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
aes_context ctx;
ctx = aes192_cbc_dec_start(p_key, iv);
aes192_cbc_dec_continue(ctx, p_data, 32);
aes192_cbc_dec_finish(ctx);
Serial.print("decrypted-cbc:");
Serial.println(p_data);
return p_data;
}
struct pl_keypair *sender, *receiver;
char clear_message[] = "dit is een test berichtje :) ";
struct pl_ctx * context;
void calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
{
// delay(100);
if(uECC_valid_public_key(p_pubkey, curve) == 1){
// Serial.println("Valid pubkey");
uECC_shared_secret(p_pubkey, p_privkey, sharedSecret, curve);
// hashSecret(sharedSecret);
}
// else
// Serial.println("INVALID pubkey");
// delay(100);
//return sharedSecret;
}
void setup()
{
lcd.init();
lcd.println("dit is een pager");
Serial.begin(9600); // Debugging only
Serial.println("RX init");
if (!manager.init())
Serial.println("init failed");
// randomSeed(analogRead(0));
Serial.print("freeMemory()=");
Serial.println(freeMemory());
uECC_set_rng(&RNG);
lcd.init();
lcd.println("dit is een pager");
Serial.begin(9600); // Debugging only
Serial.println("RX init");
if (!manager.init())
Serial.println("init failed");
Serial.print("freeMemory()=");
Serial.println(freeMemory());
context = pl_init();
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
}
void display(char *msg)
......@@ -172,57 +64,15 @@ void display(char *msg)
void loop()
{
// generateKeys(); //function to generate keys, have to make a program for this to run on a computer
// pager vars
char *decryptedMessage;
// crypto vars //
uint8_t remotePubkey[NUM_ECC_DIGITS*2];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
memcpy(receiver->private_key, tx_priv_key, ECC_COMPRESSED_SIZE-1);
memcpy(receiver->compressed_point, tx_comp_p, ECC_COMPRESSED_SIZE);
// Radio vars //
uint8_t receivedData[MESSAGE_SIZE];
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
uint8_t from;
uint8_t to;
uint8_t id;
manager.waitAvailable();
if (manager.recvfrom(buf, &buflen, &from, &to ,&id))
{
Serial.print("got message from : ");
Serial.print(from, DEC);
Serial.print(" : ");
Serial.print(id);
Serial.println(" : ");
if (id == 50){ // receiving a public key...
for (int i = 0 ; i < NUM_ECC_DIGITS+1 ; i++){
compressedPubkey[i] = buf[i];
}
uECC_decompress(compressedPubkey, remotePubkey, curve);
calcSharedSecret(remotePubkey, privkey);
}
//get msg with radio
if (id == 51){ // receiving an encrypted message...
for(int i = 0 ; i < MESSAGE_SIZE; i++){
receivedData[i] = buf[i];
}
decryptAES((char*)receivedData, sharedSecret);
}
context->kp = receiver;
pl_receive_message(context);
Serial.println("decrypted msg:");
Serial.println(context->msg->msg);
if (id == 52){ // receiving a complete message (pubkey and message)...
for (int i = 0 ; i < NUM_ECC_DIGITS+1 ; i++){ // first 25 byts is the compressed uECC pubkey
compressedPubkey[i] = buf[i];
}
uECC_decompress(compressedPubkey, remotePubkey, curve);
calcSharedSecret(remotePubkey, privkey); // then 32 bytes of encrypted message
for(int i=NUM_ECC_DIGITS+1; i < NUM_ECC_DIGITS+33; i++){
receivedData[i-(NUM_ECC_DIGITS+1)] = buf[i];
}
decryptedMessage = decryptAES((char*)receivedData, sharedSecret);
display(decryptedMessage);
}
}
}
#include <micro-ecc.h>
#include <uECC.h>
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
......@@ -17,192 +17,71 @@
//#define MESSAGE_SIZE 64
//#define PAGER_MESSAGE_SIZE 99
uint8_t tx_priv_key[ECC_COMPRESSED_SIZE-1] = {0xB4, 0x15, 0x70, 0x3E, 0xA7, 0x5D, 0x06, 0xF2, 0x33, 0x75, 0x8E, 0xE0, 0x86, 0x1B, 0x73, 0xBE, 0xEC, 0x87, 0x36, 0x0F, 0xF5, 0xE1, 0x79, 0x76, 0x2C, 0x3C, 0x74, 0x69, 0x83, 0x71, 0x07, 0xB5};
uint8_t tx_comp_p[ECC_COMPRESSED_SIZE] = {0x02, 0xA3, 0x91, 0x16, 0x71, 0x1E, 0x7B, 0xB2, 0x51, 0x7F, 0xD4, 0xF4, 0xC7, 0x81, 0x49, 0x75, 0x8B, 0x48, 0x75, 0x59, 0x27, 0xC9, 0x94, 0x3F, 0x59, 0xDD, 0xFF, 0x2E, 0x89, 0xE9, 0xD8, 0x1D, 0x2B };
RH_ASK driver(2000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
uint8_t sharedSecret[NUM_ECC_DIGITS];
uint8_t iv[16];
pl_pagermessage myMessage;
// uECC keys 192bit
uint8_t pubkey[NUM_ECC_DIGITS*2] = {0x67, 0xCF, 0x6C, 0x6F, 0x64, 0x19, 0xCC, 0xBF, 0x44, 0x60, 0x36, 0x2C, 0x99, 0x1D, 0x8C, 0x38, 0xFB, 0x6D, 0x18, 0xF2, 0x24, 0xEC, 0x8F, 0x8, 0xAB, 0x23, 0x76, 0xC, 0x4F, 0xA4, 0x63, 0x55, 0x26, 0xF8, 0x30, 0x9A, 0xA7, 0x6C, 0x55, 0x54, 0x10, 0x31, 0x0, 0xCA, 0x55, 0xF9, 0xC1, 0xFA};
uint8_t privkey[NUM_ECC_DIGITS+1] = {0xF6, 0x8B, 0x9B, 0x7F, 0xD9, 0xF8, 0xF7, 0x8C, 0x1, 0x1F, 0x6F, 0xF1, 0x9A, 0x6D, 0xC, 0xFC, 0xD4, 0x78, 0xBE, 0x26, 0x85, 0x78, 0xFD, 0xCA, 0xF4};
uint8_t rx_priv_key[ECC_COMPRESSED_SIZE-1] = {0x63, 0x36, 0xDD, 0xF1, 0x1E, 0xC2, 0xA1, 0xCF, 0x35, 0xD1, 0x79, 0x8E, 0xD5, 0xA3, 0x82, 0x1F, 0x86, 0x70, 0x8C, 0x5A, 0x09, 0x0D, 0xBA, 0xC9, 0xC5, 0xC4, 0x00, 0xE5, 0xA5, 0x7F, 0x16, 0xBA};
uint8_t rx_comp_p[ECC_COMPRESSED_SIZE] = {0x03, 0xAF, 0x52, 0x05, 0xBF, 0xC5, 0x74, 0xF8, 0xD5, 0xA9, 0xEC, 0x36, 0x65, 0x1B, 0x8F, 0x4A, 0x73, 0x0B, 0x12, 0x2E, 0x89, 0xA6, 0x9B, 0x5F, 0xA4, 0x18, 0x1E, 0x03, 0x5E, 0x95, 0xB1, 0x5F, 0x9F};
struct pl_keypair *sender, *receiver;
char clear_message[] = "dit is een test berichtje :) ";
uint8_t remotePubkey[NUM_ECC_DIGITS*2] = {0x99, 0x61, 0xB5, 0x38, 0xB3, 0x83, 0x7E, 0xFB, 0xD9, 0x3F, 0x71, 0xA3, 0x81, 0x77, 0xB0, 0x48, 0x32, 0x29, 0x24, 0x6B, 0x76, 0x48, 0x9C, 0x7A, 0x70, 0xFD, 0x3F, 0xC4, 0xB8, 0xAB, 0x8E, 0xCD, 0x31, 0x88, 0x50, 0x2D, 0xE6, 0x53, 0x49, 0xE8, 0xC0, 0xB4, 0xB5, 0xC6, 0x4F, 0x97, 0x7F, 0x6B};
static int RNG(uint8_t *dest, unsigned size) {
// Use the least-significant bits from the ADC for an unconnected pin (or connected to a source of
// random noise). This can take a long time to generate random data if the result of analogRead(0)
// doesn't change very frequently.
while (size) {
uint8_t val = 0;
for (unsigned i = 0; i < 8; ++i) {
int init = analogRead(0);
int count = 0;
while (analogRead(0) == init) {
++count;
}
if (count == 0) {
val = (val << 1) | (init & 0x01);
} else {
val = (val << 1) | (count & 0x01);
}
}
*dest = val;
++dest;
--size;
}
// NOTE: it would be a good idea to hash the resulting random data using SHA-256 or similar.
return 1;
}
uint8_t * generateIV()
{
Serial.print("IV:");
uint8_t iv[NUM_ECC_DIGITS];
static uint8_t *ptr_iv = iv;
for( int i=0 ; i < NUM_ECC_DIGITS; i++){
RNG(ptr_iv, 1);
Serial.print(iv[i] ,DEC);
Serial.print(" ");
ptr_iv++;
}
Serial.println("");
return ptr_iv;
}
void hashSecret(uint8_t *p_secret)
{
Serial.println("Secret:");
for( int i=0; i < NUM_ECC_DIGITS; i++){
Serial.print(p_secret[i]);
Serial.print(" ");
}
Serial.println("");
}
char * encryptAES(char* p_data, uint8_t *p_key, uint8_t *p_iv)
{
// Serial.println("encrypting with key:");
// hashSecret(p_key);
// uint8_t iv[] = {0,1,2,3,4,5,6,7,122,9,10,11,12,13,14,15};
aes192_cbc_enc(p_key, p_iv, p_data, 32);
Serial.print("encrypted-cbc:");
Serial.println(p_data);
return p_data;
}
struct pl_ctx * context;
char* decryptAES(char* p_data, uint8_t *p_key)
{
uint8_t iv[] = {5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
aes_context ctx;
ctx = aes192_cbc_dec_start(p_key, iv);
aes192_cbc_dec_continue(ctx, p_data, 32);
aes192_cbc_dec_finish(ctx);
// Serial.print("decrypted-cbc:");
// Serial.println(p_data);
return p_data;
}
uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
{
delay(100);
if(uECC_valid_public_key(p_pubkey, curve) == 1){
// Serial.println("Valid pubkey");
uECC_shared_secret(p_pubkey, p_privkey, sharedSecret, curve);
// hashSecret(sharedSecret);
}
//return sharedSecret;
delay(100);
}
void sendPubkey()
{
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
uECC_compress(pubkey, compressedPubkey, curve);
manager.setHeaderId(50);
manager.sendto((uint8_t *)compressedPubkey, NUM_ECC_DIGITS+1, 2);
manager.waitPacketSent();
Serial.println("sending public key...");
}
RH_ASK driver(2000);
RHDatagram manager(driver, ADDRESS);
void sendMessage(char* msg)
{
Serial.println("Sending message...");
manager.setHeaderId(51);
manager.sendto((uint8_t *)msg, MESSAGE_SIZE, 2);
manager.waitPacketSent();
}
void sendAll(char* msg)
{
// Serial.println("sending");
uint8_t pagerMsg[PAGER_MESSAGE_SIZE];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
char *encData;
//uint8_t *iv;
uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
// uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve);
uECC_compress(pubkey, compressedPubkey, curve);
// iv = generateIV();;
encData = encryptAES(msg, sharedSecret, iv);
for(int i=0; i < NUM_ECC_DIGITS+1; i++){ // put the compressed pubkey in the first 25 bytes
pagerMsg[i] = compressedPubkey[i];
}
for(int i=NUM_ECC_DIGITS+1; i < NUM_ECC_DIGITS+33; i++){ // put the encrypted message in the next 32 bytes
pagerMsg[i] = msg[i-(NUM_ECC_DIGITS+1)];
}
manager.setHeaderId(52);
manager.sendto((uint8_t *)pagerMsg, PAGER_MESSAGE_SIZE, 2);
// manager.sendto((uint8_t *)pagerMsg, PAGER_MESSAGE_SIZE, 2);
manager.waitPacketSent();
}
void messageInput()
{
byte byteRead;
char data[32];
int i = 0;
while( i < 32){
if (Serial.available() > 0) {
byteRead = Serial.read();
if(byteRead==13){ // return presses, send message and break
Serial.println();
calcSharedSecret(remotePubkey, privkey);
sendAll(data);
break;
}
else
data[i] = byteRead;
Serial.write(byteRead);
i++;
}
}
}
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("TX init");
if (!manager.init())
Serial.println("init failed");
Serial.print("freeMemory()=");
Serial.println(freeMemory());
Serial.begin(9600); // Debugging only
Serial.println("TX init");
if (!manager.init())
Serial.println("init failed");
//Serial.print("freeMemory()=");
//Serial.println(freeMemory());
context = pl_init();
sender = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
receiver = (struct pl_keypair *) malloc(sizeof(struct pl_keypair));
}
void loop()
{
// messageInput();
Serial.println("start");
generateIV();
// uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data[32] = "Hallo dit is een test";
calcSharedSecret(remotePubkey, privkey);
sendAll(data);
// delay(2000);
Serial.println("start");
Serial.print("freeMemory()=");
Serial.println(freeMemory());
//pl_load_key(sender, "sender.keypair"); // load tx keypair
//pl_load_key(receiver, "receiver.keypair"); // load tx keypair
memcpy(sender->private_key, rx_priv_key, ECC_COMPRESSED_SIZE-1);
memcpy(sender->compressed_point, rx_comp_p, ECC_COMPRESSED_SIZE);
memcpy(receiver->private_key, tx_priv_key, ECC_COMPRESSED_SIZE-1);
memcpy(receiver->compressed_point, tx_comp_p, ECC_COMPRESSED_SIZE);
context->kp = sender;
memcpy(context->msg->msg, clear_message, MSG_SIZE);
memcpy(&context->receiver_compressed_point, &receiver->compressed_point, sizeof(context->receiver_compressed_point));
pl_send_message(context);
//receive the message
context->kp = receiver;
pl_receive_message(context);
Serial.println("decrypted msg:");
Serial.println(context->msg->msg);
//free(sender);
//free(receiver);
//free(context);
delay(200);
}
CFILES=blink.c
LIBCFILES=
CFLAGS=-Os -pedantic -std=gnu99
ELF=blink.elf
HEX=blink.hex
OPTIMIZE = -O3
MCU=atmega328p
......@@ -12,11 +10,17 @@ AVRDUDE_PROGRAMMERID=stk200 /* Programmer ids are listed in /etc/avrdude.conf *
AVRDUDE_PORT=/dev/ttyUSB4
blink: blink.c
avr-gcc $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) $(CFILES) $(LIBCFILES) -o $(ELF)
avr-objcopy -j .text -j .data -O ihex $(ELF) $(HEX)
avr-gcc blink.c $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) $(LIBCFILES) -o blink.elf
avr-objcopy -j .text -j .data -O ihex blink.elf blink.hex
rm blink.elf
lib-test: lib-test.c
avr-gcc lib-test.c $(OPTIMZE) -g -I AVRLIB/ -mmcu=$(MCU) -D$(F_CPU) $(CFLAGS) $(LIBCFILES) -o lib-test.elf
avr-objcopy -j .text -j .data -O ihex lib-test.elf lib-test.hex
rm lib-test.elf
upload:
avrdude -carduino -p atmega328p -P /dev/ttyUSB1 -b57600 -U flash:w:$(HEX)
avrdude -carduino -p atmega328p -P /dev/ttyUSB1 -b57600 -U flash:w:lib-test.hex
clean:
rm -r $(ELF) $(HEX)
rm -r *.elf *.hex
......@@ -10,20 +10,44 @@
char shared_secret[SHARED_SECRET_SIZE];
char decompressed_point[64];
int main()
void hex_dump(struct pl_keypair *keypair)
{
struct pl_keypair *receiver, *sender;
struct pl_ctx * context;
context = pl_init(context);
printf("private key:\n");
printf("{");
for(int i=0; i < ECC_COMPRESSED_SIZE-1; i++){
printf("0x%02X, ", keypair->private_key[i]);
}
printf("}\ncompressed point:\n{");
for(int i=0; i < ECC_COMPRESSED_SIZE; i++){
printf("0x%02X, ", keypair->compressed_point[i]);
}
printf("}\n\n");
}
pl_create_keypair(context, sender);
pl_save_key(sender,"sender.keypair");
pl_create_keypair(context, receiver);
pl_save_key(receiver,"receiver.keypair");
int main()
{
struct pl_keypair *receiver, *sender;
struct pl_ctx * context;
context = pl_init(context);
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));
pl_create_keypair(context, sender);
pl_save_key(sender,"sender.keypair");
pl_create_keypair(context, receiver);
pl_save_key(receiver,"receiver.keypair");
printf("sender key:\n");
hex_dump(sender);
printf("receiver key:\n");
hex_dump(receiver);
return 0;
}
......@@ -29,13 +29,13 @@ int main() {
struct pl_ctx * context;
context = pl_init(context);
/*
context = pl_init();
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));
*/
int f = 0;
while ( 1 ) {
......
......@@ -5,9 +5,12 @@
#include <stdlib.h>
#include "things.h"
#include "pagerlib.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef ARDUINO
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
unsigned char fakeiv[] = "123456789abcdefghijklmnop";
......
......@@ -18,6 +18,10 @@ extern "C"
#ifdef ARDUINO
#include <AESLib.h>
#else
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"
#endif
#ifdef DEBUG
......@@ -35,10 +39,6 @@ extern "C"
#define DBM
#endif
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/aes.h"