Struct + AES +RF24 Help!!!

Salve a tutti. Ho la necessità di inviare tramite moduli nrf24l01+ degli struct di dati in maniera crittografata.
Quindi i passaggi sono i seguenti:

Struct --> AES CBC 256 bit Encrypt --> nrf24l01+ (1) ---radio---> nrf24l01+ (2) --> AES CBC 256 bit Decrypt --> Struct originale

Dal momento che devo dare in pasto dei byte all'AES non so come procedere.
La libreria AES è questa: new AES library - Libraries - Arduino Forum
La libreria RF24 (per completezza, la so usare): RF24: RF24 Class Reference

Vorrei crittografare ad esempio questo Struct:

struct dataStruct {
String Data1 = "c5390e6ad642d182fe46e36c226589ff";
  unsigned long Data2 = 0215157474;
  byte Data3[16] = { 0xf3, 0x44, 0x81, 0xec, 0x3c, 0xc6, 0x27, 0xba, 0xcd, 0x5d, 0xc3, 0xfb, 0x08, 0xf2, 0x73, 0xe6, };
  uint64_t Data4 = 0x9281fc47b5LL;
  byte Data5 = 0x44;
} pacchetto;

Quindi crittografarlo con AES CBC a 256 bit (convertire in byte? l'aes vuole un byte plain[] = {0x00, 0x00, ...}), inviarlo ad un'altro arduino il quale lo decrittografa e lo legge come lo struct originale quindi...

Serial.print(pacchetto.Data1)
Etc...

E' fattibilie?

Grazie in anticipo!
Buona serata!

Sono riuscito a crittografarlo in questo modo:

byte* plain = reinterpret_cast<byte*>(&stdPkt);
  • ovviamente i vari for loop per crittografare i blocchi

La crittografia va a buon fine ma a messaggio decrittografato (che corrisponde a quello in chiaro per fortuna) mi ritrovo in mano dei byte e non so come ricostruire lo struct.

Allo stesso modo:

dataStruct *ds = reinterpret_cast<dataStruct *> (plain);

Da lì hai un puntatore alla tua struttura, per accedere ai suoi campi usa l'operatore ->.

Ciao SukkoPera :slight_smile: e grazie per la risposta. Scusa ma non ho ben capito come implementare l'operatore. Ora posto tutto il codice:

#include <AES.h>

AES aes ;

struct dataStruct {
  String Data1 = "c5390e6ad642d182fe46e36c226589ff";
  unsigned long Data2 = 9728667411;
  unsigned long Data3 = 1968371867;
  byte Data4[16] = { 0xf3, 0x44, 0x81, 0xec, 0x3c, 0xc6, 0x27, 0xba, 0xcd, 0x5d, 0xc3, 0xfb, 0x08, 0xf2, 0x73, 0xe6, };
  uint64_t Data5 = 0x9281fc47b5LL;
  byte Data6 = 0x44;
  byte Data7 = 0xFF;
  byte Data8 = 0xE3;
  byte Data9 = 0x00;
  byte Data10[16] = { 0xf3, 0x44, 0x81, 0xec, 0x3c, 0xc6, 0x27, 0xba, 0xcd, 0x5d, 0xc3, 0xfb, 0x08, 0xf2, 0x73, 0xe6, };
} stdPkt;

byte key[] = 
{
  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
} ;

byte* plain = reinterpret_cast<byte*>(&stdPkt);

byte my_iv[] = 
{
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
} ;

byte cipher [8*N_BLOCK] ;
byte check [8*N_BLOCK] ;

void loop () 
{}


void setup ()
{
  Serial.begin (57600) ;
  Serial.print ("testng mode") ;

  prekey_test () ;
  
//  otfly_test () ;
//  otfly_test256 () ;
}

void prekey (int bits, int blocks)
{
  byte iv [N_BLOCK] ;
  
  long t0 = micros () ;
  byte succ = aes.set_key (key, bits) ;
  long t1 = micros()-t0 ;
  Serial.print ("set_key ") ; Serial.print (bits) ; Serial.print (" ->") ; Serial.print ((int) succ) ;
  Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;
  t0 = micros () ;
  if (blocks == 1)
    succ = aes.encrypt (plain, cipher) ;
  else
  {
    for (byte i = 0 ; i < 16 ; i++)
      iv[i] = my_iv[i] ;
    succ = aes.cbc_encrypt (plain, cipher, blocks, iv) ;
  }
  t1 = micros () - t0 ;
  Serial.print ("encrypt ") ; Serial.print ((int) succ) ;
  Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;
  
  t0 = micros () ;
  if (blocks == 1)
    succ = aes.decrypt (cipher, plain) ;
  else
  {
    for (byte i = 0 ; i < 16 ; i++)
      iv[i] = my_iv[i] ;
    succ = aes.cbc_decrypt (cipher, check, blocks, iv) ;
  }
  t1 = micros () - t0 ;
  Serial.print ("decrypt ") ; Serial.print ((int) succ) ;
  Serial.print (" took ") ; Serial.print (t1) ; Serial.println ("us") ;

  for (byte ph = 0 ; ph < (blocks == 1 ? 3 : 4) ; ph++)
  {
    for (byte i = 0 ; i < (ph < 3 ? blocks*N_BLOCK : N_BLOCK) ; i++)
    {
      byte val = ph == 0 ? plain[i] : ph == 1 ? cipher[i] : ph == 2 ? check[i] : iv[i] ;
      Serial.print (val>>4, HEX) ; Serial.print (val&15, HEX) ; Serial.print (" ") ;
    }

  Serial.println();
  }
    //ORA RICOMPILA LA STRUTTURA DI stdPkt DA "check" E SCRIVI SUL SERIALE UNO DEI VALORI
    Serial.println (stdPkt????????????) ;
}

void prekey_test ()
{
  prekey (256, 8) ;
}

Dove dovrei inserirlo? Scusa se sono insistente ma voglio capire il funzionamento esatto della cosa.

Altra cosa che ho modificato a sentimento e non so se è corretto.... però funziona....

byte cipher [8*N_BLOCK] ;
byte check [8*N_BLOCK] ;

Prima erano

byte cipher [4*N_BLOCK] ;
byte check [4*N_BLOCK] ;

In seriale esce:

testng modeset_key 256 ->0 took 138us
encrypt 0 took 1113us
decrypt 0 took 1911us
50 15 07 20 20 00 00 00 20 00 00 00 13 AF DF 43 9B F8 52 75 F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 B5 47 FC 81 92 00 00 00 44 FF E3 00 F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 0E 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E2 63 63 63 E2 63 63 63 E2 63 63 63 E2 63 63 63 98 FB FB FB 
ED E5 08 16 F6 EA 95 C7 63 DE 8C 8F 4D E1 89 B0 DC E8 9E 6E EF 1C EE DE 19 FC F3 12 79 87 8F 87 DA 22 24 7A EC 92 79 35 46 6B 73 50 B4 1A 35 9B C3 11 01 BA C5 F2 6C 4E 2F 09 25 91 1D 41 AD 08 0B 21 BE 6D 5F 2B 80 C2 B8 76 27 E6 73 DF 04 88 09 B5 85 89 F2 BE 2D 6B 60 14 14 C4 7A D0 7C 29 18 D9 37 CD C5 09 FD 67 CF C6 05 3E 01 FE 4C 95 02 F9 DE 07 61 27 7F 4D 09 F2 24 AF 2D E4 40 28 
50 15 07 20 20 00 00 00 20 00 00 00 13 AF DF 43 9B F8 52 75 F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 B5 47 FC 81 92 00 00 00 44 FF E3 00 F3 44 81 EC 3C C6 27 BA CD 5D C3 FB 08 F2 73 E6 00 00 00 00 0E 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 E2 63 63 63 E2 63 63 63 E2 63 63 63 E2 63 63 63 98 FB FB FB 
02 F9 DE 07 61 27 7F 4D 09 F2 24 AF 2D E4 40 28

Durante la compilazione da il seguente errore:

aes2.ino:7:25: warning: large integer implicitly truncated to unsigned type [-Woverflow]

   unsigned long Data2 = 9728667411;

                         ^