TEA encryption algorithm test vector

hello everybody,
I'm suppose to use arduino as cryptographic device for receiving then encipher data using TEA encryption algorithmhttp://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
After that transmit the data wirelessly .
What i need to know is how to set a test vector of TEA in arduino C language for encryption and decryption.
second what is the code for sending data to the serial port and the code of receiving it from serial port through RF module.

TEA sorce code for arduino is below:

// ENCRYPTION 
void encrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0=v[0], v1=v[1], sum=0, i; /* set up */
    unsigned int delta=0x9e3779b9; /* a key schedule constant */
    unsigned int k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
    for (i=0; i < 32; i++) { /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    } /* end cycle */
    v[0]=v0; v[1]=v1;
}

//DECRYPTION:

void decrypt (unsigned int* v, unsigned int* k) {
    unsigned int v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* set up */
    unsigned int delta=0x9e3779b9; /* a key schedule constant */
    unsigned int k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
    for (i=0; i<32; i++) { /* basic cycle start */
        v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
        v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        sum -= delta;
    } /* end cycle */
    v[0]=v0; v[1]=v1;
}

Are you sure your versions of the code are correct? this cypher works on 2x32 bit values, or a 64-bit block. Whereas your functions take 2x16 bit values and:

unsigned int delta=0x9e3779b9;

0x9e3779b9 is too large for the integer type.

for (i=0; i < 32; i++) {

This line is the same as the 64-bit version, I'd imagine yours would have half the iterations.

I'm sorry about that.
It is unsigned long delta=0x9e3779b9
and all of the variables are

unsigned long

The correct code is

// ENCRYPTION 
void encrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0=v[0], v1=v[1], sum=0, i; /* set up */
    unsigned long delta=0x9e3779b9; /* a key schedule constant */
    unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
    for (i=0; i < 32; i++) { /* basic cycle start */
        sum += delta;
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
    } /* end cycle */
    v[0]=v0; v[1]=v1;
}

//DECRYPTION:

void decrypt (unsigned long* v, unsigned long* k) {
    unsigned long v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* set up */
    unsigned long delta=0x9e3779b9; /* a key schedule constant */
    unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
    for (i=0; i<32; i++) { /* basic cycle start */
        v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
        v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        sum -= delta;
    } /* end cycle */
    v[0]=v0; v[1]=v1;
}

What i need to know is how to set a test vector of TEA in arduino C language for encryption and decryption.

I don't fully understand this question. You have a function that encrypts some data. Looking at that function, you can figure out what the two arguments need to look like to call the function.

second what is the code for sending data to the serial port and the code of receiving it from serial port through RF module.

It's exactly the same code as for un-encrypted data. Surely, you've seen an example or 2 billion of sending and receiving serial data, using the Serial instance.

TEA takes in pairs of 32 bit numbers and encrypts them based on a 128 bit key. If you have a stream of bytes, you need to break this down into 64 bit chunks, and then send each chunk as a pair of 32 bit values.
e.g.

#define DATA_LENGTH 40 //must be a factor of 8 to ensure whole number of 64 bit chunks
unsigned char dataToEncrypt[DATA_LENGTH]; //Fill this up with some random bytes
unsigned long* pData;
unsigned long key[4]={0x12345678,0x90123456,0x78901234,0x56789012}; //Your encryption key, 4*32 bit values=128 bits

pData=(unsigned long*)dataToEncrypt;

for(char n=0; n<DATA_LENGTH/4;n+=2){
   encrypt(pData+n,key);
}

First of all I want to Thank you all for response,

I don't fully understand this question. You have a function that encrypts some data. Looking at that function, you can figure out what the two arguments need to look like to call the function.

my problem was with the function parameters ( the function accept pointers ).

It's exactly the same code as for un-encrypted data. Surely, you've seen an example or 2 billion of sending and receiving serial data, using the Serial instance.

I have 2 LCDs in each unit of the system . One for the plaintext and the other for the cyphertext. What i need is to send the plaintext to first one and the cyphertext to the other one . The opposite process will happen at the receiving unit (the 1st LCD will display the cyphertext and the 2nd will display decyphered text ( plaintext)). But, how can i synchronous the displaying process in each unit.

If you have a stream of bytes, you need to break this down into 64 bit chunks, and then send each chunk as a pair of 32 bit values

Would you please explain it more ? How could I do that ?

#define DATA_LENGTH 40 //must be a factor of 8 to ensure whole number of 64 bit chunks

Why it must be a factor of 8? would you please explain it more?

unsigned char dataToEncrypt[DATA_LENGTH]; //Fill this up with some random bytes

like this

unsigned char dataToEncrypt[DATA_LENGTH]={"America" , "NewYourk",...etc };

TEA encrypts your data in blocks of 64 bits. The smallest piece of memory you can address is an 8 bit byte, therefore you need to break your data up into blocks of 8 bytes (if it's less than 8 bytes long, or doesn't round to a whole number then you need to pad the data, this is often done with 0s).
What data are you actually trying to encrypt, what I had described was for testing the algorithm with some random data, depending what data you want to send you will want to change it accordingly.

I'm still waiting ?!!!!!!!!!!!!!!!!!!!!!!!

thanks tobyb121.
What I want to do is testing my cryptosystem with some random data.
I am trying to send encrypted stream of data to RF transmitter that connect to pin 3 at the transmission unit. Then Receive the stream at receiving unit then decrypt it.

I don't know what more you need to know. I've given you enough information to at least make a stab at building something, I know you want your whole program written out for you to copy&paste, but I'm not going to do that, you won't learn anything and when it inevitably doesn't quite work we'll be in no better position than we are at the moment. I'm happy to answer any (specific) questions you have, but you should have a go at building it, it would be a good exercise in understanding how pointers work and understanding how to work with c style strings properly.

Apparently there are TEA and XTEA test vectors here: Test Vectors for TEA and XTEA

From what I remember the original TEA has a weak key schedule, make sure you have the version with a
fixed key schedule, or use XTEA or XXTEA.

Also have you seen this library (caveat, I haven't downloaded or used it). Arduino/libraries/Xtea at master · franksmicro/Arduino · GitHub

I know you want your whole program written out for you to copy&paste[/quote
Thank you tobyb121 for your help .Before showing the difficulties That I'm facing, I want to say that I'm not like those kind of guys who want to copy&past.
My first step is here:

#define DATA_LENGTH 40

unsigned char dataToEncrypt[DATA_LENGTH]={ 0xgf12hd11 , .....etc };
unsigned long* ptrToData;
unsigned long key[4]={0x12345678,0x90123456,0x78901234,0x56789012};
ptrToData=(unsigned long*)dataToEncrypt;
for(char n=0; n<DATA_LENGTH/4;n+=2){
  encrypt(ptrToData+n,key);
}



So this was the test vector of encryption unit. After the encryption process finish,The encrypted data that stored at (dataToEncrypt) needs to be transmitted through RF module to the decryption unit. I will use for loop to transmitted .
The problem is at the decryption unit. how could I receive the stream . RF module that I use transmit pockets with 8 bits long (only 4 of them is data ).

Thank you MarkT.
I have seen this test vector before, but I does not work for my system. Because my system needs two separate test vectors. One at the encryption unit for transmitting the encrypted data. the second at the decryption unit for receiving the encrypted data then decrypt them.

Firstly 0xgf12hd11 is not a valid unsigned char value, they should be two hex digits.

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.

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:
char Str6[15] = {"Welcome","to","Arduino","World"};
What should I modify at the following :

ptrToData=(unsigned long*)dataToEncrypt;
for(char n=0; n<DATA_LENGTH/4;n+=2){
   encrypt(ptrToData+n,key);
}

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 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.

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.

Here is my final code . It sends and receives data but not the data what I expect.
I sent

unsigned long dataToEncrypt [2] = {0x01234567, 0x89abcdef};

I must receive

{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:

#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

#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);
 
  }

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.

Eng-Osama:
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:

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