Pages: 1 [2]   Go Down
Author Topic: TEA encryption algorithm test vector  (Read 1646 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Firstly 0xgf12hd11 is not a valid unsigned char value, they should be two hex digits.
That's right It must be like ( 0x23 , 0x87 ... etc ).
 If I want to replace it with this code:
 
Code:
char Str6[15] = {"Welcome","to","Arduino","World"};
What should I modify at the following :
Code:
ptrToData=(unsigned long*)dataToEncrypt;
for(char n=0; n<DATA_LENGTH/4;n+=2){
   encrypt(ptrToData+n,key);
}


Quote
What do you mean by your RF module sending 8 bit packets where only 4 are data?? Do you have a data sheet? Have you tested just sending a string between the two arduinos.

I'm sorry . I just skimmed the VirtualWire documentation quickly and there was misunderstanding .The 4 bits was for the encoding process." the byte in the messageis encoded as 2x6 bit symbols".

The maximum packet length consists of 408 bits .
 
Here is The official VirtualWire documentation
Code:
http://www.airspayce.com/mikem/arduino/VirtualWire.pdf
Witch is the Library that I'm using in Arduino IDE to send and receive data through the RF module.
« Last Edit: May 15, 2013, 05:41:39 pm by Eng-Osama » Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you got it working just sending basic strings? If you've got two bytes, you can get the lower four bits by and-ing with 0x0F and the higher 4 bits by and-ing with 0xF0 then right shifting by 4.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my final code . It sends and receives data but not the data what I expect.
I sent
Code:
unsigned long dataToEncrypt [2] = {0x01234567, 0x89abcdef};
I must receive
Code:
{0x126c6b92 , 0xc0653a3e}
But I did not get what I want. I do not Know where is the problem at the program logic or somewhere else.
TX Code:
Code:
#include <LiquidCrystal.h>
#include <VirtualWire.h>
#include <SoftwareSerial.h>


// Decleration Part
#define DATA_LENGTH 64 // bits number of data is .
// data to encrypt
unsigned long dataToEncrypt [2] = {0x01234567, 0x89abcdef};
// The Key of encryption and decryption
unsigned long key [4] = {0x00112233,0x44556677,0x8899aabb,0xccddeeff};
// The encrypted data that is ready to send.
unsigned char dataToSend[8];
// pointer to dataToEncrypt
//unsigned long* ptrToData;
//Data pointer initialization
//ptrToData = (unsigned long*) dataToEncrypt;
// pointer to Key
//unsigned long* ptrToKey;
//Key pointer initialization
//ptrToData =(unsigned long*) key;


void setup() {
 
   Serial.begin (9600);
  // Initialise the IO and ISR
  vw_set_tx_pin(8);  // RF Transmitter pin data will connect to pin #8.
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000); // Bits per sec
 
}
// TEA Encryption Function  code:


void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0=v[0], v1=v[1], sum=0;int i; /* set up */
    unsigned long delta=0x9e3779b9; /* a key schedule constant */
    unsigned long key0=key[0], key1=key[1], key2=key[2], key3=key[3]; /* cache key */
    for (i=0; i < 32; i++) { /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + key0 ^ (v1 + sum) ^ ((v1>>5) + key1));
        v1 += ((v0<<4) + key2 ^ (v0 + sum) ^ ((v0>>5) + key3));
    } /* end cycle */
       v[0]=v0; v[1]=v1;
}// end of the encryption function


void loop()
{
// char ascii[32];
//sprintf(ascii, "original: 0x%08X 0x%08X \n",(int)dataToEncrypt[0],(int)dataToEncrypt[1]); // ?????????
 // print the plain text to the serial port
 //Serial.print(ascii);
 
 // Calling the encryption function
 encrypt(dataToEncrypt,key);
 
// sprintf(ascii,"encrypted:0x%08X 0x%08X \n",(int)dataToEncrypt[0],(int)dataToEncrypt[1]); // ??????????
 // print the cypher text to the serial port
 //Serial.print((int)dataToEncrypt ,HEX);
 //delay(10000);
 
 // To chuncks the cypher text array to char type to send it through RF module using virtualWire library
 // Convert 2x32 array to 8x8 array.
 
 unsigned const long hexToByte[4]={ 0xff000000,0x00ff0000,0x0000ff00,0x000000ff};
 
 int j = 0 ;  // To count dataToSend elements
 while ( j < 8 ){
 for ( char i = 0 ; i < 2; i++){
   for ( char c = 0 ; c < 4; c++){
     
     dataToSend[j] = dataToEncrypt [i] & hexToByte[c];
     j++;
   } // end of the inner for loop.
  }// end of the outer loop.
 }// enf of while loop.   
 
 
 // start data transmitting through the RF Module.
  vw_send((uint8_t *)dataToSend, 8);
  vw_wait_tx();   // Wait for message to finish
 
  delay(2000);    // delay befor start the the void loop() again.
 
 
}
RX code
Code:
#include <LiquidCrystal.h>
#include <VirtualWire.h>
#include <SoftwareSerial.h>


// Decleration Part
#define DATA_LENGTH 64 // bits number of data is .
// data to decrypt
unsigned long dataToDecrypt [2];
// The Key of encryption and decryption
unsigned long key [4] = {0x00112233,0x44556677,0x8899aabb,0xccddeeff};
// The encrypted data that is ready to receive.
unsigned char dataToReceive[8];
// pointer to dataToDecrypt
//unsigned long* ptrToData;
//Data pointer initialization
//ptrToData =(unsigned long*) dataToDecrypt;
// pointer to Key
//unsigned long* ptrToKey;
//Key pointer initialization
//ptrToData =(unsigned long*) key;
unsigned long recivedData[8];


void setup() {
 
   Serial.begin (9600);   // boud per second for the serial port
  // Initialise the IO and ISR
  vw_set_rx_pin(9);  //  // We will be receiving on pin 9
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000); // Bits per sec
  vw_rx_start();         // Start the receiver 
}

// TEA Decryption Function  code:
void decrypt (unsigned long* v, unsigned long* key) {
    unsigned long v0=v[0], v1=v[1], sum=0xC6EF3720; int i ; /* set up */
    unsigned long delta=0x9e3779b9; /* a key schedule constant */
    unsigned long key0=key[0], key1=key[1], key2=key[2], key3=key[3]; /* cache key */
    for (i=0; i<32; i++) { /* basic cycle start */
        v1 -= ((v0<<4) + key2) ^ (v0 + sum) ^ ((v0>>5) + key3);
        v0 -= ((v1<<4) + key0) ^ (v1 + sum) ^ ((v1>>5) + key1);
        sum -= delta;
    } // end of the decryption function
     v[0]=v0; v[1]=v1;

void loop(){

 unsigned const long byteToHEX=0x000000ff; 
  char ascii[32];
  // "buf" is an arry to hold the rceived chunks ( Byte size )
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  // "buflen" is byte size variable holds the max size of the message upon transmiting.
  // Then during return "buflen" will hold the actual size of the received message.
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  // check to see if anything has been received
  if (vw_get_message(buf, &buflen)){
   Serial.print("RECEIVING PROCESS STARTS ! \n");
   Serial.print("Got: ");
   
   // Print the received chunks to computer.
   for (int i = 0; i < buflen; i++)
{
    Serial.print(buf[i], HEX);
    Serial.print(' ');
}
Serial.println();
  }
   // Group the received chunks of byte data type  into dataToDecrypt array of unsigned long data type.
   
   
    // copy the data from "buf" array to "recivedData" array
    for( char m = 0 ; m < 8 ; m++){
    recivedData[m] = (((unsigned long) buf[m]) &( byteToHEX));
    } // end of copy loop
   
    // Note the RF module send the LSB first.
    for ( char k = 0 ; k < 8 ; k ++){
      dataToDecrypt[0]= (recivedData[k]) | ((recivedData[k+1]) << 1) | ((recivedData[k+2]) << 2) | ((recivedData[k+3]) << 3) ;
      dataToDecrypt[1]= (recivedData[k+4]) | ((recivedData[k+5]) << 1) | ((recivedData[k+6]) << 2) | ((recivedData[k+7]) << 3) ;
    }
 // Calling the encryption function
 decrypt(dataToDecrypt,key);
 sprintf(ascii,"decrypted:0x%08X 0x%08X \n",(int)dataToDecrypt[0],(int)dataToDecrypt[1]); // ??????????
  //print the cypher text to the serial port
  for (int i = 0 ; i<2 ;i++){
 Serial.print(dataToDecrypt[i] , HEX);
 Serial.print(" - \n");}
 delay(1000);
 
  }   
     
     
   
   
 
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's impossible to tell what the problem could be as there are too many unknowns. Break your code up so that you can treat each bit individually. Start by making a program that sends plain characters with the RF module and ensure that you can reconstruct it at the other end correctly. Then put this into a function that takes an arbitrary array of bytes, again test with simple data to ensure it works. When you have that function created and working, your encryption and sending become independent of each other, for example if the output is incorrect then it must be the encryption, you will know where to look.
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char Str6[15] = {"Welcome","to","Arduino","World"};
Looks like odd syntax to me, char Str6[15] allocates an array of char with 15 elements, {"Welcome","to","Arduino","World"} is an array of pointers to chars, i.e. a char**, so these types don't match.
The way you should do it is create a single char array, with the length being a factor of 8:
Code:
char Str6[32]="Welcome to Arduino World";
Even though "Welcome to Arduino World" is only 20 something characters long it allocates 32 bytes, to ensure 64 bit blocks, and the remaining bytes are all padded with 0s
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you tobyb121.
I am working on that now.
Logged

Pages: 1 [2]   Go Up
Jump to: