```
/****************************************************************/
/* 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;
}
```