Commit 64bfb91a authored by yids's avatar yids

reworked a lot of stuff, crypto and radio seem to work simultaniously,...

reworked a lot of stuff, crypto and radio seem to work simultaniously, decrypting a received message still doesnt work probably something with data types or sizes
parent d11c8deb
// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
#include <stdarg.h>
#include <uECC.h>
#include <AESLib.h>
......@@ -16,8 +10,6 @@
#include <LCDI2C_LK162-12.h>
#include <MemoryFree.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
LCDI2C lcd = LCDI2C(2,16,0x50,0);
......@@ -28,68 +20,13 @@ aes_context ctx; // context for the cbc crypto stuff
RH_ASK driver(5000);
RHDatagram manager(driver, ADDRESS);
// uECC keys 256bit
/*
uint8_t privkey[NUM_ECC_DIGITS+1] = {0xDB, 0x5A, 0xDF, 0xBD, 0xFD, 0xE3, 0x25, 0xD2, 0xB6, 0x5C, 0x6A, 0xEA, 0xAF, 0xC4, 0xC4, 0xD9, 0x7B, 0x5E, 0x99, 0xD6, 0x66, 0xEC, 0xA0, 0xA4, 0x6F, 0xB, 0xBD, 0x42, 0x25, 0xEE, 0x3E, 0x40, 0xF4};
uint8_t pubkey[NUM_ECC_DIGITS*2] = {0x12, 0x70, 0x13, 0x5, 0x57, 0xAD, 0xDD, 0x9C, 0x9D, 0xC, 0x60, 0x2F, 0xE2, 0xC4, 0x9C, 0x4, 0x99, 0x77, 0x2D, 0x22, 0xA6, 0x46, 0x9C, 0xC6, 0xE4, 0x7D, 0x4C, 0x9D, 0x7E, 0x1F, 0xD6, 0x94, 0xC5, 0x23, 0xF1, 0x21, 0x32, 0x4B, 0x6B, 0xB, 0x65, 0xB, 0x57, 0x7E, 0x66, 0x1D, 0xA5, 0x41, 0x7, 0xF5, 0xE2, 0x1C, 0x38, 0x98, 0xFB, 0x95, 0xBA, 0x1B, 0xA3, 0x2B, 0x35, 0x26, 0x1D, 0xF6};
*/
const struct uECC_Curve_t * curve = CURVE;
uint8_t sharedSecret[NUM_ECC_DIGITS];
// uECC keys 192bit
uint8_t pubkey[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};
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};
uint8_t* generateIV()
{
uint8_t buffer[BLOCK_SIZE];
Serial.println("generating IV...");
for (int i=0 ; i<BLOCK_SIZE ; i++){
buffer[i] = random(64);
}
for (int i = 0; i < 17; i++){
Serial.print(buffer[i]);
}
Serial.println("");
return buffer;
}
char* encryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
{
Serial.print("data:");
Serial.println(p_data);
Serial.print("encrypting data...");
aes128_cbc_enc_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_enc_finish(ctx);
Serial.println("done");
return p_data;
}
char* decryptAES(uint8_t* p_secret, char* p_data, const aes_context ctx)
{
Serial.print("decrypting data...");
aes128_cbc_dec_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_dec_finish(ctx);
Serial.println("done");
return p_data;
}
/*
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t calcSharedSecret(uint8_t *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS], uECC_Curve curve)
{
Serial.print("Calculating shared secret...");
uint8_t secret[24];
unsigned long a = millis();
int r = uECC_shared_secret(p_pubkey, p_privkey, secret, curve);
unsigned long b = millis();
Serial.print("Shared secret 1 in "); Serial.println(b-a);
if (!r) {
Serial.print("shared_secret() failed (1)\n");
return 0;
}
return *secret;
}
*/
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)
......@@ -146,6 +83,54 @@ void generateKeys()
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;
}
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;
}
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);
}
delay(100);
//return sharedSecret;
}
void setup()
{
lcd.init();
......@@ -160,27 +145,17 @@ void setup()
uECC_set_rng(&RNG);
}
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("");
}
void loop()
{
// generateKeys(); //function to generate keys, have to make a program for this to run on a computer
// crypto vars //
uint8_t remotePubkey[48];
uint8_t sharedSecret[NUM_ECC_DIGITS];
const struct uECC_Curve_t * curve = CURVE;
// uint8_t sharedSecret[NUM_ECC_DIGITS];
//const struct uECC_Curve_t * curve = CURVE;
// Radio vars //
uint8_t* receivedData;
char receivedData[32];
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
uint8_t from;
......@@ -198,36 +173,16 @@ void loop()
for (int i = 0 ; i < NUM_ECC_DIGITS*2 ; i++){
remotePubkey[i] = buf[i];
}
calcSharedSecret(remotePubkey, privkey);
}
if (id == 51){ // receiving an encrypted message...
Serial.println("Storing received message");
receivedData = buf;
Serial.println((char*)receivedData);
for(int i = 0 ; i < 32; i++){
receivedData[i] = buf[i];
}
//decryptAES(receivedData, sharedSecret);
}
}
if(uECC_valid_public_key(remotePubkey, curve) == 1){
Serial.println("Valid pubkey");
uECC_shared_secret(remotePubkey, privkey, sharedSecret, curve);
hashSecret(sharedSecret);
}
else
Serial.println("Invalid pubkey");
/* // CBC Crypto //
char* decryptedData;
// uint8_t* iv = generateIV();
uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
ctx = aes128_cbc_dec_start(&sharedSecret, iv);
decryptedData = decryptAES(&sharedSecret, ((char*)receivedData), ctx);
Serial.print("decrypted data:");
Serial.println(decryptedData);
Serial.println ("");
*/
/*
// single block crypto //
aes128_dec_single(&sharedSecret, receivedData);
Serial.println((char*)receivedData);
*/
}
BOARD_TAG=atmega328
USER_LIB_PATH=../libs/
ARDUINO_LIBS=RadioHead SPI AESLib micro-ecc
ARDUINO_LIBS=RadioHead SPI AESLib micro-ecc MemoryFree
include /usr/share/arduino/Arduino.mk
DEVICE_PATH = /dev/ttyUSB1
......
// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
#include <uECC.h>
#include <AESLib.h>
#include <RadioHead.h>
#include <RH_ASK.h>
#include <RHDatagram.h>
#include <SPI.h> // Not actually used but needed to compile
#include <MemoryFree.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 4
#define NUM_BLOCKS 1
#define ADDRESS 5
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
aes_context ctx;
RH_ASK driver(5000);
RHDatagram manager(driver, ADDRESS);
// uECC keys 256bit
/*
uint8_t privkey[NUM_ECC_DIGITS+1] = {0x59, 0x7F, 0xF2, 0x59, 0xC4, 0x3E, 0xD3, 0xDE, 0x76, 0xDF, 0xD6, 0xB1, 0x6A, 0x7F, 0x75, 0xE3, 0x49, 0xC4, 0x3F, 0xDB, 0x29, 0xAB, 0xDC, 0xDC, 0x67, 0x4A, 0x7F, 0xEF, 0xB, 0xF2, 0xA5, 0xFB, 0xF4};
uint8_t pubkey[NUM_ECC_DIGITS*2] = {0x5C, 0x54, 0x4F, 0x7D, 0x8B, 0x59, 0x58, 0x9C, 0x10, 0xDF, 0xD2, 0x63, 0x88, 0x7F, 0xFB, 0x2C, 0x93, 0xF8, 0x5F, 0xFD, 0xF6, 0x24, 0x1, 0xFE, 0x97, 0xEB, 0x64, 0xED, 0x2, 0xC1, 0x13, 0x4B, 0x86, 0x79, 0xE0, 0x5A, 0x34, 0xE4, 0xFB, 0x6E, 0xDA, 0xC8, 0x37, 0xA5, 0xDB, 0xC7, 0x9F, 0x5E, 0xAC, 0xA1, 0x75, 0x54, 0xA2, 0x15, 0xB6, 0x33, 0x97, 0x27, 0x53, 0x93, 0x81, 0x34, 0x5B, 0xDE};
const struct uECC_Curve_t * curve = CURVE;
uint8_t sharedSecret[NUM_ECC_DIGITS];
uint8_t remotePubkey[NUM_ECC_DIGITS*2] = {0x12, 0x70, 0x13, 0x5, 0x57, 0xAD, 0xDD, 0x9C, 0x9D, 0xC, 0x60, 0x2F, 0xE2, 0xC4, 0x9C, 0x4, 0x99, 0x77, 0x2D, 0x22, 0xA6, 0x46, 0x9C, 0xC6, 0xE4, 0x7D, 0x4C, 0x9D, 0x7E, 0x1F, 0xD6, 0x94, 0xC5, 0x23, 0xF1, 0x21, 0x32, 0x4B, 0x6B, 0xB, 0x65, 0xB, 0x57, 0x7E, 0x66, 0x1D, 0xA5, 0x41, 0x7, 0xF5, 0xE2, 0x1C, 0x38, 0x98, 0xFB, 0x95, 0xBA, 0x1B, 0xA3, 0x2B, 0x35, 0x26, 0x1D, 0xF6};
*/
#define BLOCK_SIZE 16
#define NUM_BLOCKS 1
// 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};
......@@ -36,64 +28,50 @@ uint8_t privkey[NUM_ECC_DIGITS+1] = {0xF6, 0x8B, 0x9B, 0x7F, 0xD9, 0xF8, 0xF7, 0
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};
uint8_t* generateIV()
char * encryptAES(char* p_data, uint8_t *p_key)
{
uint8_t buffer[BLOCK_SIZE];
Serial.println("generating IV...");
for (int i=0 ; i<BLOCK_SIZE ; i++){
buffer[i] = random(64);
}
for (int i = 0; i < 17; i++){
Serial.print(buffer[i]);
}
Serial.println("");
return buffer;
}
char* encryptAES(char* p_data, const aes_context ctx)
{
Serial.print("data:");
Serial.println(p_data);
Serial.print("encrypting data...");
aes128_cbc_enc_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_enc_finish(ctx);
Serial.println("done");
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;
}
char* decryptAES(char* p_data, const aes_context ctx)
char* decryptAES(char* p_data, uint8_t *p_key)
{
Serial.print("decrypting data...");
aes128_cbc_dec_continue(ctx, p_data, BLOCK_SIZE*NUM_BLOCKS);
aes128_cbc_dec_finish(ctx);
Serial.println("done");
return p_data;
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;
}
/*
// calculate shared secret using remote public key and local private key
// returns the shared secret
uint8_t * calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey, uECC_Curve curve)
void hashSecret(uint8_t *p_secret)
{
Serial.print("Calculating shared secret...");
uint8_t secret[24];
unsigned long a = millis();
int r = uECC_shared_secret(p_pubkey, p_privkey, secret, curve);
unsigned long b = millis();
Serial.print("Shared secret 1 in "); Serial.println(b-a);
if (!r) {
Serial.print("shared_secret() failed (1)\n");
return 0;
Serial.println("Secret:");
for( int i=0; i < NUM_ECC_DIGITS; i++){
Serial.print(p_secret[i]);
Serial.print(" ");
}
return secret;
Serial.println("");
}
*/
void setup()
uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
{
Serial.begin(9600); // Debugging only
Serial.println("TX init");
if (!manager.init())
Serial.println("init failed");
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);
}
delay(100);
//return sharedSecret;
}
void sendPubkey()
......@@ -109,48 +87,38 @@ void sendMessage(char* msg)
Serial.println("Sending message...");
//char* msg = "hallo";
manager.setHeaderId(51);
// manager.sendto((uint8_t *)msg, 48, 2);
manager.sendto((uint8_t *)msg, 32, 2);
manager.waitPacketSent();
}
void hashSecret(uint8_t *p_secret)
void setup()
{
Serial.println("Secret:");
for( int i=0; i < NUM_ECC_DIGITS; i++){
Serial.print(p_secret[i]);
Serial.print(" ");
}
Serial.println("");
Serial.begin(9600); // Debugging only
Serial.println("crypto test");
if (!manager.init())
Serial.println("init failed");
Serial.print("freeMemory()=");
Serial.println(freeMemory());
}
void loop()
{
// crypto vars //
uint8_t sharedSecret[NUM_ECC_DIGITS];
const struct uECC_Curve_t * curve = CURVE;
char* data = "Hallo";
char* encryptedData;
uint8_t iv[16] = {4,9,4,9,4,9,4,9,4,9,4,9,4,9,4,9};
uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
char data[] = "Hallo dit is een test";
char *encData;
char *decData;
Serial.print("freeMemory()=");
Serial.println(freeMemory());
sendPubkey();
if(uECC_valid_public_key(remotePubkey, curve) == 1){
Serial.println("Valid pubkey");
uECC_shared_secret(remotePubkey, privkey, sharedSecret, curve);
hashSecret(sharedSecret);
}
else
Serial.println("Invalid pubkey");
// ctx = aes128_cbc_enc_start(&sharedSecret, iv);
// encryptedData = encryptAES(data, ctx);
//
// delay(10000);
// sendMessage(data);
// delay(5000);
delay(100);
encData = encryptAES(data, sharedSecret);
// delay(500);
sendMessage(encData);
decData = decryptAES(encData, sharedSecret);
delay(5000);
}
CC=gcc
CFLAGS=-I ../include
LIBS=../include/ecc.c ../include/oaes_base64.c ../include/oaes_lib.c ../include/rand.c
LIBS=../include/ecc.c
base-tx: base-tx.c
$(CC) -o base-tx base-tx.c $(LIBS) $(CFLAGS)
#include <ecc.h>
#include "../include/oaes_lib.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stddef.h>
#include <openssl/aes.h>
#define BLOCK_SIZE 16
//my pub-priv keypair
uint8_t privkey[NUM_ECC_DIGITS] = {0x14, 0x14, 0x92, 0x2D, 0x2E, 0x00, 0x87, 0x16, 0x2D, 0x43, 0x0E, 0xC1, 0x8A, 0xD0, 0x0A, 0x1E, 0xEE, 0x89, 0x4F, 0x17, 0x3F, 0xB1, 0x1B, 0x5A};
......@@ -49,11 +51,15 @@ uint8_t calcSharedSecret(EccPoint *p_pubkey, uint8_t p_privkey[NUM_ECC_DIGITS])
int main(int argc, char **argv)
{
// ECC //
size_t testje;
uint8_t sharedSecret;
printf("Basestation TX test\n");
checkPubkey(&remotePubkey);
sharedSecret = calcSharedSecret(&remotePubkey, privkey);
return 0;
AES_KEY enc_key;
}
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