Dear All,

Many thanks for the answer of my first question though i had the answer for this from one who has already put this question forward.I have modified it and it has worked.Thanks!!
Now i want to optimize my coding by adding another condition so that it doesn't perform a loop unnecessary if the same data is passed everytimes.Can i cache a result somewhere in C!Should i use an if statement?
In fact i have 3 test cases in 1 module.But two of them have same data to be passed.so i want to cache an intermediate result whenever the same data is used.

Many thanks

Nalini

>Can i cache a result somewhere in C!
Yes, you can do just about anything in C.

>Should i use an if statement?
I'm sure you would need one somewhere. :icon_rolleyes:

>so i want to cache an intermediate result whenever the same data is used.
So what's the problem?

Hello

I dont know how to cache the result that's why i am asking.And about the if statement i have tried to add one but am getting error while compiling.

Grateful if you could help

thanks

Nalini

>I dont know how to cache the result that's why i am asking.
Caching is just saving data in memory for later use. It could be as simple as this:

int x;
int oldx;
int result;

...

if ( x != oldx ) {
  /* Don't bypass the calculation */
  result = ...
}

oldx = x;

printf ( "Result = %d\n", result );

Dear Narue

many thanks for all ur positive answers!will try to mail u the coding if i happen to shorten it.But if i cant i will mail u the whole!!please do help me..i will make and add those statements..but do correct me !
you seems to be really a guru in C!i have gone thru ur tutorials in C.thanks again

thanking you

Nal

Dear Narue

i am mailing u the coding but its ok if u cant go over it as i know its very long!:( i have removed the steps that need nt be optimised .But have pointed out whre the conditions need to be placed as i dont know hw to put it.It makes heavy use of pointers which i am learning slowly.I have 2 more to see by myself but this one seems to be the most difficult.

Just hope u can help me partly at least or explain to me hw this can be done.
thanks

Best regards

Nal

Attachments
/****************************************************************/
/* This code is made freely available. Do with it as you will.  */
/****************************************************************/
 
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <stdio.h>




/********************************************/
/* Test Cases                               */
/* An array of test cases                   */
/********************************************/
#define NUM_TEST_CASES  4


                                                           
unsigned long int test_case_pnl[] = {                /* least significant 2 bytes of pn */
                                        0x0000,
                                        0x0001
                                        
                                    };
unsigned long int test_case_pnh[] = {               /* most significant 4 bytes of pn */
                                        0x00000000, 
                                        0x00000000
                                        
                                    };

unsigned char keys[] =
{
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
    0x63,0x89,0x3B,0x25,0x08,0x40,0xB8,0xAE,0x0B,0xD0,0xFA,0x7E,0x61,0xD2,0x78,0x3E,
    0x63,0x89,0x3B,0x25,0x08,0x40,0xB8,0xAE,0x0B,0xD0,0xFA,0x7E,0x61,0xD2,0x78,0x3E
    

};

unsigned char transmitter_addr[] =
{
    0x10,0x22,0x33,0x44,0x55,0x66,
    0x10,0x22,0x33,0x44,0x55,0x66,
    0x64,0xF2,0xEA,0xED,0xDC,0x25,
    0x64,0xF2,0xEA,0xED,0xDC,0x25
    

};





/*****************************/
/**** Function Prototypes ****/
/*****************************/



/* Mixes key from TA, TK and TSC */ 

void mix_key(   unsigned char   *key,
                unsigned char   *ta,
                unsigned long int pnl,  /* Least significant 16 bits of PN */
                unsigned long int pnh,  /* Most significant 32 bits of PN */
                unsigned char   *rc4key,
                unsigned int *p1kout
            );


/****************************************************/
/* mix_key()                                        */
/* Takes a key, PN and TK. Calculates an RC4 key.   */
/****************************************************/

unsigned int rotr1(unsigned int a)
{
    unsigned int b;

    if ((a & 0x01) == 0x01)
    {
        b = (a >> 1) | 0x8000;
    }
    else
    {
        b = (a >> 1) & 0x7fff;
    }
    b = b % 65536;
    return b;
}

void mix_key(   unsigned char   *key,
                unsigned char   *ta,
                unsigned long int pnl,  /* Least significant 16 bits of PN */
                unsigned long int pnh,  /* Most significant 32 bits of PN */
                unsigned char   *rc4key,
                unsigned int *p1k
            )
{
    /* 16 bit numbers */
    unsigned int tsc0;
    unsigned int tsc1;
    unsigned int tsc2;

    unsigned int ppk0;
    unsigned int ppk1;
    unsigned int ppk2;
    unsigned int ppk3;
    unsigned int ppk4;
    unsigned int ppk5;

    int i;
    int j;

    tsc0 = (unsigned int)((pnh >> 16) % 65536);  /* tsc0 is most significant */
    tsc1 = (unsigned int)(pnh % 65536);
    tsc2 = (unsigned int)(pnl % 65536);          /* tsc2 is least significant */

    /* Phase 1, step 1: this is the part that i think shd be done once if Ta for any case is same */
    p1k[0] = tsc1;
    p1k[1] = tsc0;
    p1k[2] = (unsigned int)(ta[0] + (ta[1]*256));
    p1k[3] = (unsigned int)(ta[2] + (ta[3]*256));
    p1k[4] = (unsigned int)(ta[4] + (ta[5]*256));



    /* Phase 1, step 2 */
    for (i=0; i<8; i++)
    {
        j = 2*(i & 1);
        p1k[0] =  (p1k[0] + tkip_sbox( (p1k[4] ^ ((256*key[1+j]) + key[j])) % 65536 )) % 65536;
        p1k[1] =  (p1k[1] + tkip_sbox( (p1k[0] ^ ((256*key[5+j]) + key[4+j])) % 65536 )) % 65536;
        p1k[2] =  (p1k[2] + tkip_sbox( (p1k[1] ^ ((256*key[9+j]) + key[8+j])) % 65536 )) % 65536;
        p1k[3] =  (p1k[3] + tkip_sbox( (p1k[2] ^ ((256*key[13+j]) + key[12+j])) % 65536 )) % 65536;
        p1k[4] =  (p1k[4] + tkip_sbox( (p1k[3] ^ (((256*key[1+j]) + key[j]))) % 65536 )) % 65536;
        p1k[4] = (p1k[4] + i) % 65536;
    }


    /* Phase 2, Step 1 */
    ppk0 = p1k[0];
    ppk1 = p1k[1];
    ppk2 = p1k[2];
    ppk3 = p1k[3];
    ppk4 = p1k[4];
    ppk5 = (p1k[4] + tsc2) % 65536;

    /* Phase2, Step 2 */
    ppk0 = ppk0 + tkip_sbox( (ppk5 ^ ((256*key[1]) + key[0])) % 65536);
    ppk1 = ppk1 + tkip_sbox( (ppk0 ^ ((256*key[3]) + key[2])) % 65536);
    ppk2 = ppk2 + tkip_sbox( (ppk1 ^ ((256*key[5]) + key[4])) % 65536);
    ppk3 = ppk3 + tkip_sbox( (ppk2 ^ ((256*key[7]) + key[6])) % 65536);
    ppk4 = ppk4 + tkip_sbox( (ppk3 ^ ((256*key[9]) + key[8])) % 65536);
    ppk5 = ppk5 + tkip_sbox( (ppk4 ^ ((256*key[11]) + key[10])) % 65536);

    ppk0 = ppk0 + rotr1(ppk5 ^ ((256*key[13]) + key[12]));
    ppk1 = ppk1 + rotr1(ppk0 ^ ((256*key[15]) + key[14]));
    ppk2 = ppk2 + rotr1(ppk1);
    ppk3 = ppk3 + rotr1(ppk2);
    ppk4 = ppk4 + rotr1(ppk3);
    ppk5 = ppk5 + rotr1(ppk4);

    /* Phase 2, Step 3 */
    rc4key[0] = tsc2 % 256; 
    rc4key[1] = (((tsc2 / 256) % 256) | 0x20) & 0x7f;
    rc4key[2] = (tsc2 / 256) % 256; 
    rc4key[3] = ((ppk5 ^ ((256*key[1]) + key[0])) >> 1) % 256;

    rc4key[4] = ppk0 % 256;
    rc4key[5] = (ppk0 / 256) % 256;

    rc4key[6] = ppk1 % 256;
    rc4key[7] = (ppk1 / 256) % 256;

    rc4key[8] = ppk2 % 256;
    rc4key[9] = (ppk2 / 256) % 256;

    rc4key[10] = ppk3 % 256;
    rc4key[11] = (ppk3 / 256) % 256;

    rc4key[12] = ppk4 % 256;
    rc4key[13] = (ppk4 / 256) % 256;

    rc4key[14] = ppk5 % 256;
    rc4key[15] = (ppk5 / 256) % 256;
}

/****************************************************/
/* main()                                           */
/* Iterate through the test cases, passing them     */
/* through the TKIP algorithm to produce test       */
/* vectors and verify decryption against encryption */
/****************************************************/

int main()
{
    int test_case;
    unsigned char *key;
    unsigned char *ta;
    unsigned char rc4key[16];
    unsigned int p1k[5];


    unsigned int i;
   clock_t start = clock();

    for (i=0; i<16;i++) rc4key[i] = 0x00;

    for (test_case = 1; test_case < (NUM_TEST_CASES+1); test_case++)
    {
        printf ("\nTest vector #%d:\n",test_case);
        key = keys + (16 * (test_case-1));
        ta = transmitter_addr + (6 * (test_case-1));

        printf("TK    =");
        for (i=0; i<16; i++)
        {
            printf(" %02X", key[i]);
        }
        printf(" [LSB on left, MSB on right]\n");

        printf("TA    =");
/*if Ta and TK same as previous test case then phase 1 step 1 need not be done for this test case */
        printf(" %02X", ta[0]);
        for (i=1; i<6; i++)
        {
            printf("-%02X", ta[i]);
        }
        printf("\n");

        printf("PN    = %08lX%04lX [transmitted as: ",
                test_case_pnh[test_case-1],
                test_case_pnl[test_case-1]);
        printf(" %02lX %02lX Reserved DefKeyID",
                (test_case_pnl[test_case-1] % 256),
                ((test_case_pnl[test_case-1] >> 8) % 256)
            );
        printf(" %02lX %02lX %02lX %02lX]\n",
                (test_case_pnh[test_case-1] % 256),
                ((test_case_pnh[test_case-1] >> 8) % 256),
                ((test_case_pnh[test_case-1] >> 16) % 256),
                ((test_case_pnh[test_case-1] >> 24) % 256)

            );

        printf("IV32  = %08lX\n", test_case_pnh[test_case-1]);
        printf("IV16  = %04lX\n", test_case_pnl[test_case-1]);
         

        mix_key(    key,
                    ta,
                    test_case_pnl[test_case-1],
                    test_case_pnh[test_case-1],
                    rc4key,
                    p1k
                    );

        printf("P1K   =");
        for (i=0; i<5; i++)
        {
            printf(" %04X", p1k[i]);
        }
        printf("\n");
        printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);

        printf("RC4KEY=");
        for (i=0; i<16; i++)
        {
            printf(" %02X", rc4key[i]);
        }
        

        printf("\n");
        

    }

    return 0;
}
This article has been dead for over six months. Start a new discussion instead.