Commit 23d4b0cb authored by yids's avatar yids

- pubkey and encrypted message are now sent in one transmission

parent d98e662c
Pipeline #25 skipped
......@@ -18,8 +18,9 @@ aes_context ctx; // context for the cbc crypto stuff
#define ADDRESS 2
#define MESSAGE_SIZE 32
#define PAGER_MESSAGE_SIZE 57
RH_ASK driver(3000);
RH_ASK driver(2000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
......@@ -87,12 +88,12 @@ void generateKeys()
void hashSecret(uint8_t *p_secret)
{
// Serial.println("Secret:");
Serial.println("Secret:");
for( int i=0; i < NUM_ECC_DIGITS; i++){
// Serial.print(p_secret[i]);
// Serial.print(" ");
Serial.print(p_secret[i]);
Serial.print(" ");
}
// Serial.println("");
Serial.println("");
}
char * encryptAES(char* p_data, uint8_t *p_key)
......@@ -111,7 +112,7 @@ char* decryptAES(char* p_data, uint8_t *p_key)
{
delay(100);
// Serial.println("Decrypting with key:");
hashSecret(sharedSecret);
// 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);
......@@ -129,10 +130,11 @@ void calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
if(uECC_valid_public_key(p_pubkey, curve) == 1){
// Serial.println("Valid pubkey");
uECC_shared_secret(p_pubkey, p_privkey, sharedSecret, curve);
hashSecret(sharedSecret);
// hashSecret(sharedSecret);
}
else
// Serial.println("INVALID pubkey");
delay(100);
// Serial.println("Calculating shared secret finished");
//return sharedSecret;
}
......@@ -157,42 +159,49 @@ void loop()
// crypto vars //
uint8_t remotePubkey[NUM_ECC_DIGITS*2];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
// uint8_t sharedSecret[NUM_ECC_DIGITS];
//const struct uECC_Curve_t * curve = CURVE;
// Radio vars //
char receivedData[32];
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;
//if(sharedSecret[0] != 0 && receivedData[0] != 0){decryptAES((char*)receivedData, sharedSecret);}
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(" : ");
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];
}
//if(sharedSecret[0] == 0){calcSharedSecret(remotePubkey, privkey);}
uECC_decompress(compressedPubkey, remotePubkey, curve);
calcSharedSecret(remotePubkey, privkey);
}
if (id == 51){ // receiving an encrypted message...
// Serial.println("Storing received message");
for(int i = 0 ; i < 32; i++){
for(int i = 0 ; i < MESSAGE_SIZE; i++){
receivedData[i] = buf[i];
}
//if(sharedSecret[0] !=0){decryptAES((char*)receivedData, sharedSecret);}
decryptAES((char*)receivedData, sharedSecret);
}
}
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];
}
decryptAES((char*)receivedData, sharedSecret);
}
}
}
......@@ -6,22 +6,18 @@
#include <SPI.h> // Not actually used but needed to compile
#include <MemoryFree.h>
#define BLOCK_SIZE 16
#define NUM_BLOCKS 1
#define ADDRESS 5
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
#define MESSAGE_SIZE 32
#define PAGER_MESSAGE_SIZE 57
RH_ASK driver(3000);
RH_ASK driver(2000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
uint8_t sharedSecret[NUM_ECC_DIGITS];
#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};
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};
......@@ -40,8 +36,8 @@ void hashSecret(uint8_t *p_secret)
char * encryptAES(char* p_data, uint8_t *p_key)
{
Serial.println("encrypting with key:");
hashSecret(p_key);
// Serial.println("encrypting with key:");
// hashSecret(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);
......@@ -59,8 +55,8 @@ char* decryptAES(char* p_data, uint8_t *p_key)
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);
// Serial.print("decrypted-cbc:");
// Serial.println(p_data);
return p_data;
}
......@@ -68,9 +64,9 @@ 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");
// Serial.println("Valid pubkey");
uECC_shared_secret(p_pubkey, p_privkey, sharedSecret, curve);
hashSecret(sharedSecret);
// hashSecret(sharedSecret);
}
delay(100);
//return sharedSecret;
......@@ -78,10 +74,10 @@ uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
void sendPubkey()
{
uint8_t compressedPubkey[25];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
uECC_compress(pubkey, compressedPubkey, curve);
manager.setHeaderId(50);
manager.sendto((uint8_t *)compressedPubkey, 25, 2);
manager.sendto((uint8_t *)compressedPubkey, NUM_ECC_DIGITS+1, 2);
manager.waitPacketSent();
Serial.println("sending public key...");
}
......@@ -89,35 +85,33 @@ void sendPubkey()
void sendMessage(char* msg)
{
Serial.println("Sending message...");
//char* msg = "hallo";
manager.setHeaderId(51);
manager.sendto((uint8_t *)msg, 32, 2);
manager.sendto((uint8_t *)msg, MESSAGE_SIZE, 2);
manager.waitPacketSent();
}
void sendAll(char* msg)
{
uint8_t pagerMsg[56];
uint8_t compressedPubkey[25];
char data[] = "Hallo dit is een test";
uint8_t pagerMsg[PAGER_MESSAGE_SIZE];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
char *encData;
uECC_compress(pubkey, compressedPubkey, curve);
encData = encryptAES(msg, sharedSecret);
Serial.println("Sending pubkey and message...");
for(int i=0; i < 24; i++){
uECC_compress(pubkey, compressedPubkey, curve);
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=23; i < 56; i++){
pagerMsg[i] = encData[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, 56, 2);
// manager.waitPacketSent();
Serial.println("done sending..");
manager.setHeaderId(52);
manager.sendto((uint8_t *)pagerMsg, PAGER_MESSAGE_SIZE, 2);
manager.waitPacketSent();
}
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("crypto test");
Serial.println("TX init");
if (!manager.init())
Serial.println("init failed");
Serial.print("freeMemory()=");
......@@ -126,25 +120,9 @@ void setup()
void loop()
{
Serial.println("start..");
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());
calcSharedSecret(remotePubkey, privkey);
// sendAll(data);
sendPubkey();
sendAll(data);
delay(5000);
encData = encryptAES(data, sharedSecret);
delay(100);
sendMessage(encData);
decData = decryptAES(encData, sharedSecret);
delay(3000);
}
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