Go Down

Topic: AES Encryption across RF (Read 311 times) previous topic - next topic

archeresque

Hello guys, I'm trying to send an encrypted packet of integers across two Arduino units using the nRF24l01+ RF antennas. I'm using this library(http://spaniakos.github.io/AES/index.html) for the encryption and this library(https://tmrh20.github.io/RF24Network/) for the RF network. How would I structure my packet and handle the encryption and decryption processes to send over a packet encrypted at one arduino and decrypted on the other? I'm having trouble listing the correct variables on each arduino and calling the functions accordingly. The end goal is to send over a packet with an ID byte and room for three int variables but for now just sending over one number or a word is fine because I'm having a little trouble understanding the set-up. I have arduino sketches I've been working on that I can post but they are pretty rough because of my lac of understanding.

Thank you for any help!

jremington

#1
Aug 10, 2017, 10:37 pm Last Edit: Aug 10, 2017, 10:37 pm by jremington
The radio doesn't know anything about the data you are sending.

First make sure you can reliably send and receive clear text data packets.

Then, replace the clear text data with the encrypted versions.

archeresque

So I have no issue sending clear text, I just have an issue re-translating the encrypted text to the true text being communicated. As far as I understand, both Arduino units just need to have some code like this to handle the decryption. This is of course separate from the code needed to establish the RF network etc.
 
Code: [Select]
unsigned int keyLength [1] = {128}; // key length: 128b
byte *key = (unsigned char*)"01234567890123456789012345678901"; // encryption key
unsigned long long int myIv = 36753562; // CBC initialization vector; real iv = iv x2 ex: 01234567 = 0123456701234567
struct payload_t {
  byte cipher[];
};

I would imagine if I include these initializations as well as the encrypt() on the transmitter and then decrypt() on the receiver, the message should be successfully sent and decrypted. I think the problem may lie in the way the encrypted text is being sent. I am loading my packet as follows
Code: [Select]
payload_t payload = {cipher};                           
    RF24NetworkHeader header(/*to node*/ home_node);
    bool ok = network.write(header,&payload,sizeof(payload));
    if (ok)
      Serial.println("ok.");
    else
      Serial.println("failed.");
where cipher is defined by
Code: [Select]
byte iv [N_BLOCK] ;
  int plainPaddedLength = sizeof(plain) + (N_BLOCK - ((sizeof(plain)-1) % 16)); // length of padded plaintext [B]
  byte cipher [plainPaddedLength]; // ciphertext (encrypted plaintext)

Thoughts on this? Sorry for text dumping and I appreciate you taking the time to help me out.

jremington

Snippets are useless. Please follow the directions and post all the code.

Again, the radio doesn't care what data are sent. The problem surely lies in how you are moving the encrypted data into the transmitted packet and back out again at the receiver.

archeresque

Here's some of my work in progress code. It's highly flawed but let me know what you think!

jremington

#5
Aug 11, 2017, 04:34 am Last Edit: Aug 11, 2017, 04:38 am by jremington
I can't even begin to imagine how this is supposed to work. The struct defines a single byte payload, but in the context of the function aesTest(), cipher[] is an array of bytes.

Code: [Select]
   
struct payload_t {                  // Structure of our payload
  byte cipher;
};
...

   payload_t payload = {cipher};


My approach would be to move bytes from a buffer containing the encrypted message, into a multibyte payload buffer, using the memcpy() function, and then verify that the received buffer contents are equal to the original encrypted data, before moving on.

archeresque

Yeah the code is a mess but thanks for helping me with the logic. I'm new to this type of programming so I appreciate you taking the time to help me through the process. I'll take a look at programming the memory buffers to properly load the payload.

jremington

#7
Aug 11, 2017, 07:28 pm Last Edit: Aug 11, 2017, 07:29 pm by jremington
You need to choose a payload buffer size, which may or may not be the same size as the encrypted message.

If the encrypted messages are always the same length in bytes, then it is simplest if the two buffers are the same, i.e. the output of the encryption routine IS the payload (subject to size limitations imposed by the radio and radio library).

archeresque

Thank you! I just got it to work after reading the library documentation and adjusting the buffer size. I really appreciate your help.

Go Up