Commit f33444f2 authored by yids's avatar yids

- removed hardcoded pubkey, they can be calculated

- added function to rx to display message on lcd
- started on iv stuff, made function to generate random iv
- encryptAES function now takes iv as an argument and is a bit simplified (removed the aes context stuff as it was unnecessary)
- started on function to control TX over serial
parent 56ea54e7
Pipeline #28 skipped
......@@ -20,6 +20,10 @@ 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);
......@@ -27,7 +31,6 @@ 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};
static int RNG(uint8_t *dest, unsigned size) {
......@@ -110,7 +113,6 @@ char * encryptAES(char* p_data, uint8_t *p_key)
char* decryptAES(char* p_data, uint8_t *p_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};
......@@ -121,20 +123,20 @@ char* decryptAES(char* p_data, uint8_t *p_key)
Serial.print("decrypted-cbc:");
Serial.println(p_data);
return p_data;
delay(100);
}
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);
// 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
// else
// Serial.println("INVALID pubkey");
delay(100);
// delay(100);
//return sharedSecret;
}
......@@ -152,9 +154,26 @@ void setup()
uECC_set_rng(&RNG);
}
void display(char *msg)
{
/* char line1[16];
char line2[16];
for(int i=0; i < MESSAGE_SIZE ; i++){
if(i < 16)
line1[i] = msg[i];
else
line2[i-16] = msg[i];
};
*/
lcd.clear();
lcd.println(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];
......@@ -200,8 +219,9 @@ void loop()
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);
decryptedMessage = decryptAES((char*)receivedData, sharedSecret);
display(decryptedMessage);
}
}
}
......@@ -12,11 +12,16 @@
#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];
uint8_t iv[16];
// 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};
......@@ -24,6 +29,49 @@ 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};
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:");
......@@ -34,23 +82,20 @@ void hashSecret(uint8_t *p_secret)
Serial.println("");
}
char * encryptAES(char* p_data, uint8_t *p_key)
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,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);
// 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;
}
char* decryptAES(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};
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);
......@@ -62,14 +107,14 @@ char* decryptAES(char* p_data, uint8_t *p_key)
uint8_t *calcSharedSecret(uint8_t *p_pubkey, uint8_t *p_privkey)
{
delay(100);
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);
}
void sendPubkey()
......@@ -91,15 +136,20 @@ void sendMessage(char* msg)
}
void sendAll(char* msg)
{
char data[] = "Hallo dit is een test";
// Serial.println("sending");
uint8_t pagerMsg[PAGER_MESSAGE_SIZE];
uint8_t compressedPubkey[NUM_ECC_DIGITS+1];
char *encData;
encData = encryptAES(msg, sharedSecret);
//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)];
}
......@@ -108,6 +158,28 @@ void sendAll(char* msg)
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
......@@ -120,9 +192,13 @@ void setup()
void loop()
{
uint8_t iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
// 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[] = "Hallo dit is een test";
calcSharedSecret(remotePubkey, privkey);
sendAll(data);
delay(5000);
// delay(2000);
}
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