Commit d98e662c authored by hark's avatar hark
parents 6719363a 064f2b6c
Pipeline #24 skipped
......@@ -17,7 +17,9 @@ LCDI2C lcd = LCDI2C(2,16,0x50,0);
aes_context ctx; // context for the cbc crypto stuff
#define ADDRESS 2
RH_ASK driver(5000);
#define MESSAGE_SIZE 32
RH_ASK driver(3000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
......@@ -85,12 +87,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)
......@@ -107,7 +109,8 @@ char * encryptAES(char* p_data, uint8_t *p_key)
char* decryptAES(char* p_data, uint8_t *p_key)
{
Serial.println("Decrypting with key:");
delay(100);
// 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;
......@@ -117,18 +120,20 @@ char* decryptAES(char* p_data, uint8_t *p_key)
Serial.print("decrypted-cbc:");
Serial.println(p_data);
return p_data;
delay(100);
}
uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
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");
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;
delay(100);
// Serial.println("Calculating shared secret finished");
//return sharedSecret;
}
void setup()
......@@ -150,8 +155,9 @@ 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];
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 //
......@@ -161,35 +167,32 @@ void loop()
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*2 ; i++){
remotePubkey[i] = buf[i];
for (int i = 0 ; i < NUM_ECC_DIGITS+1 ; i++){
compressedPubkey[i] = buf[i];
}
calcSharedSecret(remotePubkey, privkey);
//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");
// Serial.println("Storing received message");
for(int i = 0 ; i < 32; i++){
receivedData[i] = buf[i];
}
//if(sharedSecret[0] !=0){decryptAES((char*)receivedData, sharedSecret);}
decryptAES((char*)receivedData, sharedSecret);
}
}
/* if (sharedSecret[0] == 0){
calcSharedSecret(remotePubkey, privkey);
}
else{
decryptAES((char*)receivedData, sharedSecret);
}
*/
}
......@@ -13,7 +13,7 @@
#define NUM_ECC_DIGITS 24 //size of privkey, curvesize in bytes
#define CURVE uECC_secp192r1()
RH_ASK driver(5000);
RH_ASK driver(3000);
RHDatagram manager(driver, ADDRESS);
const struct uECC_Curve_t * curve = CURVE;
......@@ -78,8 +78,10 @@ uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
void sendPubkey()
{
uint8_t compressedPubkey[25];
uECC_compress(pubkey, compressedPubkey, curve);
manager.setHeaderId(50);
manager.sendto((uint8_t *)pubkey, 48, 2);
manager.sendto((uint8_t *)compressedPubkey, 25, 2);
manager.waitPacketSent();
Serial.println("sending public key...");
}
......@@ -92,6 +94,25 @@ void sendMessage(char* msg)
manager.sendto((uint8_t *)msg, 32, 2);
manager.waitPacketSent();
}
void sendAll(char* msg)
{
uint8_t pagerMsg[56];
uint8_t compressedPubkey[25];
char *encData;
uECC_compress(pubkey, compressedPubkey, curve);
encData = encryptAES(msg, sharedSecret);
Serial.println("Sending pubkey and message...");
for(int i=0; i < 24; i++){
pagerMsg[i] = compressedPubkey[i];
}
for(int i=23; i < 56; i++){
pagerMsg[i] = encData[i];
}
// manager.setHeaderId(52);
// manager.sendto((uint8_t *)pagerMsg, 56, 2);
// manager.waitPacketSent();
Serial.println("done sending..");
}
void setup()
{
......@@ -105,7 +126,7 @@ 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;
......@@ -115,13 +136,15 @@ void loop()
Serial.println(freeMemory());
calcSharedSecret(remotePubkey, privkey);
// sendAll(data);
sendPubkey();
delay(100);
delay(5000);
encData = encryptAES(data, sharedSecret);
// delay(500);
delay(100);
sendMessage(encData);
decData = decryptAES(encData, sharedSecret);
delay(5000);
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