/****************************************************************/ /* 802.11 AES-CCM Test Vector Generator */ /* (c) 2002, David Johnston */ /* Author: David Johnston */ /* Email (home): dj@deadhat.com */ /* Email (general): david.johnston@ieee.org */ /* Version 1.1 */ /* Includes: */ /* TGi Draft 2.3 CCM plus.. */ /* Muting Retry, More data, Pwr Mgmt, CF-Ack, CF-Poll */ /* Little Endian IV */ /* Protected Bit set for MIC calc */ /* v0.6: */ /* Built in AES encrypt function */ /* Automatic checking of CRC result */ /* Corrected DefKeyId values */ /* Test Case #9 with QoS_CTL = 0xFFFF */ /* v0.7 */ /* Changed parsing name CRC32 to FCS */ /* Fix in test case #9 */ /* - missing DefKeyID entry */ /* - Changed key to match GC's */ /* Added test case #10 received from GC. */ /* Fixed warnings from gcc -Wall */ /* Changed instances of initial_* to mic_* */ /* Changed format of PN output to a 48 bit hex integer */ /* v0.8 */ /* Made address output match 802 standard format. */ /* Corrected order of A2 within nonce */ /* Simplified output format. */ /* v0.9 */ /* Switched sbox case statement to an array. */ /* Made output format suitable for 802.11 spec. */ /* Removed dead code. */ /* v1.0 */ /* Minor clean ups */ /* Tabs removed */ /* v1.1 */ /* Changes contributed by Mark Matson */ /* Sbox switched to an array */ /* Added CONSISTENT_PN_ORDER option to: */ /* Insert the PN in the MIC_IV and CTR_PRELOAD */ /* in the same order it appears on the air */ /* (low-order byte first). */ /* v1.2 Added sequence number muting */ /****************************************************************/ #include #include /*****************************************/ /* Algorithm behaviour options */ /* Enable or disable these options to */ /* modify the behaviour of the algorithm */ /*****************************************/ /* Reverses PN order in MIC to match on air PN order. */ /* This is not standard behaviour in draft 3.0 but */ /* may become so in the future. */ /* #define CONSISTENT_PN_ORDER */ /****************************************/ /* Text output options. */ /* Comment out to reduce text output */ /****************************************/ /* Either enable this define for standard output */ #define SHOW_TGI_DRAFT_TEXT /* Or enable any selection of the following for diagnostic output #define SHOW_PHASES #define SHOW_TESTCASE_NUM #define SHOW_PARSING_TITLE #define SHOW_HEADER_FIELDS #define SHOW_HEADER #define SHOW_RSN_HEADER #define SHOW_INIT_BLOCKS #define SHOW_CTR_PRELOAD #define SHOW_MIC #define SHOW_DATA #define SHOW_CRC #define SHOW_PASSFAIL */ /********************************/ /* 802.11 FC subfield encodings */ /********************************/ #define MANAGEMENT_FRAME 0 #define CONTROL_FRAME 1 #define DATA_FRAME 2 #define RESERVED_FRAME 3 #define QOS_DATA 8 #define QOS_NULL 12 /********************************************/ /* Test Cases */ /* An array of test cases, each containing */ /* an MPDU. A separate array gives the */ /* length of each MPDU. */ /********************************************/ #define NUM_TEST_CASES 10 int test_case_length[] = {41, 41, 47, 49, 43, 41, 110, 54, 54, 34}; int test_case_defkeyid[] = {2,3,0,0,0,2,2,1,1,0}; #ifdef CONSISTENT_PN_ORDER unsigned char test_case_crc[] = { 0x6d,0xe3,0x66,0x48, 0x9c,0x8b,0x0f,0xbc, 0x15,0xed,0x14,0x42, 0x8e,0xd3,0x49,0x64, 0xf6,0x64,0x84,0x8a, 0x69,0x9a,0x35,0x30, 0xcb,0x70,0x50,0x5c, 0x7d,0x08,0x2a,0x25, 0xa9,0x8b,0x4b,0xda, 0xd0,0xf6,0x2f,0x9e }; #else unsigned char test_case_crc[] = { 0x94,0x8a,0x40,0x43, 0x65,0xe2,0x29,0xb7, 0x70,0x5a,0xd4,0x12, 0x61,0x99,0xed,0xcf, 0x03,0x24,0xe4,0x52, 0x22,0x1a,0xcc,0x8a, 0xcd,0x51,0x23,0x67, 0x60,0xbc,0xba,0x79, 0xcb,0x69,0x37,0x92, 0xf3,0x21,0x63,0x01 }; #endif unsigned long int test_case_pnl[] = { /* least significant 4 bytes of pn */ 0x04030201, 0x04030201, 0x00000001, 0x00000001, 0x00000001, 0x04030201, 0x04030201, 0x00000001, 0x00000001, 0x00000001 }; unsigned long int test_case_pnh[] = { 0x0605, 0x0605, /* most significant 2 bytes of pn */ 0x0000, 0x0000, 0x0000, 0x0605, 0x0005, 0x0000, 0x0000, 0x0000 }; unsigned char keys[] = { 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0xc9,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x98,0x3a,0x16,0xef,0x4f,0xac,0xb3,0x51,0xaa,0x9e,0xcc,0x27,0x1d,0x73,0x09,0xe2, 0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf }; unsigned char test_cases[] = /* Test Case 1 */ { 0x08,0x01, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 2 */ 0x08,0x09, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 3 */ 0x08,0x03, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ 0x41,0x42,0x43,0x44,0x45,0x46, /* A4 */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 4 */ 0x88,0x03, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ 0x41,0x42,0x43,0x44,0x45,0x46, /* A4 */ 0x04,0x00, /* QC */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 5 */ 0x88,0x01, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ 0x04,0x00, /* QC */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 6 */ 0x08,0x01, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ /* Payload */ 0xaa,0xaa,0x03,0x00,0x00,0x00,0x88,0x8e,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00, 0x00, /* Test Case 7 */ 0x08,0x03, /* FC */ 0x12,0x34, /* Duration */ 0xff,0xff,0xff,0xff,0xff,0xff, /* A1 */ 0x00,0x40,0x96,0x45,0x07,0xf1, /* A2 */ 0x08,0x00,0x46,0x17,0x62,0x3e, /* A3 */ 0x50,0x67, /* SC */ 0xaa,0xaa,0x03,0x00,0x00,0x00, /* A4 */ /* Payload */ 0x08,0x00,0x45,0x00,0x00,0x4e,0x66,0x1a,0x00,0x00,0x80,0x11,0xbe,0x64,0x0a,0x00, 0x01,0x22,0x0a,0xff,0xff,0xff,0x00,0x89,0x00,0x89,0x00,0x3a,0x00,0x00,0x80,0xa6, 0x01,0x10,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x45,0x43,0x45,0x4a,0x45, 0x48,0x45,0x43,0x46,0x43,0x45,0x50,0x46,0x45,0x45,0x49,0x45,0x46,0x46,0x43,0x43, 0x41,0x43,0x41,0x43,0x41,0x43,0x41,0x43,0x41,0x41,0x41,0x00,0x00,0x20,0x00,0x01, /* Test Case 8 */ 0xb8,0xbf, /* FC */ 0xc8,0x00, /* Duration */ 0xa1,0xa1,0xa1,0xa1,0xa1,0xa1, /* A1 */ 0xa2,0xa2,0xa2,0xa2,0xa2,0xa2, /* A2 */ 0xa3,0xa3,0xa3,0xa3,0xa3,0xa3, /* A3 */ 0x01,0xfa, /* SC */ 0xa4,0xa4,0xa4,0xa4,0xa4,0xa4, /* A4 */ 0x77,0xff, /* QC */ /* Payload */ 0x69,0x6e,0x6f,0x76,0x61,0x74,0x69,0x6f,0x6e,0x73,0x20,0x69,0x6e,0x20,0x77,0x69, 0x72,0x65,0x6c,0x65,0x73,0x73, /* Test Case 9 */ 0xb8,0xbf, /* FC */ 0xc8,0x00, /* Duration */ 0xa1,0xa1,0xa1,0xa1,0xa1,0xa1, /* A1 */ 0xa2,0xa2,0xa2,0xa2,0xa2,0xa2, /* A2 */ 0xa3,0xa3,0xa3,0xa3,0xa3,0xa3, /* A3 */ 0x01,0xfa, /* SC */ 0xa4,0xa4,0xa4,0xa4,0xa4,0xa4, /* A4 */ 0xff,0xff, /* QC */ /* Payload */ 0x69,0x6e,0x6f,0x76,0x61,0x74,0x69,0x6f,0x6e,0x73,0x20,0x69,0x6e,0x20,0x77,0x69, 0x72,0x65,0x6c,0x65,0x73,0x73, /* Test Case 10 */ 0x08,0x03, /* FC */ 0x02,0x01, /* Duration */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A1 */ 0x00,0x02,0x2d,0x49,0x97,0xb4, /* A2 */ 0x00,0x06,0x25,0xa7,0xc4,0x36, /* A3 */ 0xe0,0x00, /* SC */ 0x41,0x42,0x43,0x44,0x45,0x46, /* A4 */ /* Payload */ 0xaa,0xaa,0x03,0x00 }; /***************************************/ /********* Table for CRC 32 ************/ /***************************************/ unsigned char lookahead_table[1024] = { 0x00,0x00,0x00,0x00,0x96,0x30,0x07,0x77,0x2c,0x61,0x0e,0xee,0xba,0x51,0x09,0x99, 0x19,0xc4,0x6d,0x07,0x8f,0xf4,0x6a,0x70,0x35,0xa5,0x63,0xe9,0xa3,0x95,0x64,0x9e, 0x32,0x88,0xdb,0x0e,0xa4,0xb8,0xdc,0x79,0x1e,0xe9,0xd5,0xe0,0x88,0xd9,0xd2,0x97, 0x2b,0x4c,0xb6,0x09,0xbd,0x7c,0xb1,0x7e,0x07,0x2d,0xb8,0xe7,0x91,0x1d,0xbf,0x90, 0x64,0x10,0xb7,0x1d,0xf2,0x20,0xb0,0x6a,0x48,0x71,0xb9,0xf3,0x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e9,0xff,0xb5,0x30, 0x1c,0xf2,0xbd,0xbd,0x8a,0xc2,0xba,0xca,0x30,0x93,0xb3,0x53,0xa6,0xa3,0xb4,0x24, 0x05,0x36,0xd0,0xba,0x93,0x06,0xd7,0xcd,0x29,0x57,0xde,0x54,0xbf,0x67,0xd9,0x23, 0x2e,0x7a,0x66,0xb3,0xb8,0x4a,0x61,0xc4,0x02,0x1b,0x68,0x5d,0x94,0x2b,0x6f,0x2a, 0x37,0xbe,0x0b,0xb4,0xa1,0x8e,0x0c,0xc3,0x1b,0xdf,0x05,0x5a,0x8d,0xef,0x02,0x2d }; /*****************************/ /******** SBOX Table *********/ /*****************************/ unsigned char sbox_table[256] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; /*****************************/ /**** Function Prototypes ****/ /*****************************/ void bitwise_xor(unsigned char *ina, unsigned char *inb, unsigned char *out); void construct_mic_iv( unsigned char *mic_header1, int qc_exists, int a4_exists, unsigned char *mpdu, unsigned int payload_length, unsigned char * pn_vector ); void construct_mic_header1( unsigned char *mic_header1, int header_length, unsigned char *mpdu ); void construct_mic_header2( unsigned char *mic_header2, unsigned char *mpdu, int a4_exists, int qc_exists ); void construct_ctr_preload( unsigned char *ctr_preload, int a4_exists, int qc_exists, unsigned char *mpdu, unsigned char *pn_vector, int c ); void get_mpdu( int test_case, unsigned char *plaintext); int parse_mpdu( unsigned char *mpdu, int length, int *header_length, /* The functions may modify the */ int *payload_length, /* pass by reference variables */ int *a4_exists, int *qc_exists, unsigned char *expected_crc, int testnum); int encrypt_mpdu( unsigned char *key, unsigned long int pnl, unsigned long int pnh, unsigned int def_key_id, int length, int *header_length, int *payload_length, int a4_exists, int qc_exists, unsigned char *plaintext, unsigned char *ciphertext); void xor_128(unsigned char *a, unsigned char *b, unsigned char *out); void xor_32(unsigned char *a, unsigned char *b, unsigned char *out); unsigned char sbox(unsigned char a); void next_key(unsigned char *key, int round); void byte_sub(unsigned char *in, unsigned char *out); void shift_row(unsigned char *in, unsigned char *out); void mix_column(unsigned char *in, unsigned char *out); void add_round_key( unsigned char *shiftrow_in, unsigned char *mcol_in, unsigned char *block_in, int round, unsigned char *out); void aes128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphertext); /****************************************/ /* aes128k128d() */ /* Performs a 128 bit AES encrypt with */ /* 128 bit data. */ /****************************************/ void xor_128(unsigned char *a, unsigned char *b, unsigned char *out) { int i; for (i=0;i<16; i++) { out[i] = a[i] ^ b[i]; } } void xor_32(unsigned char *a, unsigned char *b, unsigned char *out) { int i; for (i=0;i<4; i++) { out[i] = a[i] ^ b[i]; } } unsigned char sbox(unsigned char a) { return sbox_table[(int)a]; } void next_key(unsigned char *key, int round) { unsigned char rcon; unsigned char sbox_key[4]; unsigned char rcon_table[12] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x36, 0x36 }; sbox_key[0] = sbox(key[13]); sbox_key[1] = sbox(key[14]); sbox_key[2] = sbox(key[15]); sbox_key[3] = sbox(key[12]); rcon = rcon_table[round]; xor_32(&key[0], sbox_key, &key[0]); key[0] = key[0] ^ rcon; xor_32(&key[4], &key[0], &key[4]); xor_32(&key[8], &key[4], &key[8]); xor_32(&key[12], &key[8], &key[12]); } void byte_sub(unsigned char *in, unsigned char *out) { int i; for (i=0; i< 16; i++) { out[i] = sbox(in[i]); } } void shift_row(unsigned char *in, unsigned char *out) { out[0] = in[0]; out[1] = in[5]; out[2] = in[10]; out[3] = in[15]; out[4] = in[4]; out[5] = in[9]; out[6] = in[14]; out[7] = in[3]; out[8] = in[8]; out[9] = in[13]; out[10] = in[2]; out[11] = in[7]; out[12] = in[12]; out[13] = in[1]; out[14] = in[6]; out[15] = in[11]; } void mix_column(unsigned char *in, unsigned char *out) { int i; unsigned char add1b[4]; unsigned char add1bf7[4]; unsigned char rotl[4]; unsigned char swap_halfs[4]; unsigned char andf7[4]; unsigned char rotr[4]; unsigned char temp[4]; unsigned char tempb[4]; for (i=0 ; i<4; i++) { if ((in[i] & 0x80)== 0x80) add1b[i] = 0x1b; else add1b[i] = 0x00; } swap_halfs[0] = in[2]; /* Swap halfs */ swap_halfs[1] = in[3]; swap_halfs[2] = in[0]; swap_halfs[3] = in[1]; rotl[0] = in[3]; /* Rotate left 8 bits */ rotl[1] = in[0]; rotl[2] = in[1]; rotl[3] = in[2]; andf7[0] = in[0] & 0x7f; andf7[1] = in[1] & 0x7f; andf7[2] = in[2] & 0x7f; andf7[3] = in[3] & 0x7f; for (i = 3; i>0; i--) /* logical shift left 1 bit */ { andf7[i] = andf7[i] << 1; if ((andf7[i-1] & 0x80) == 0x80) { andf7[i] = (andf7[i] | 0x01); } } andf7[0] = andf7[0] << 1; andf7[0] = andf7[0] & 0xfe; xor_32(add1b, andf7, add1bf7); xor_32(in, add1bf7, rotr); temp[0] = rotr[0]; /* Rotate right 8 bits */ rotr[0] = rotr[1]; rotr[1] = rotr[2]; rotr[2] = rotr[3]; rotr[3] = temp[0]; xor_32(add1bf7, rotr, temp); xor_32(swap_halfs, rotl,tempb); xor_32(temp, tempb, out); } void aes128k128d(unsigned char *key, unsigned char *data, unsigned char *ciphertext) { int round; int i; unsigned char intermediatea[16]; unsigned char intermediateb[16]; unsigned char round_key[16]; for(i=0; i<16; i++) round_key[i] = key[i]; for (round = 0; round < 11; round++) { if (round == 0) { xor_128(round_key, data, ciphertext); next_key(round_key, round); } else if (round == 10) { byte_sub(ciphertext, intermediatea); shift_row(intermediatea, intermediateb); xor_128(intermediateb, round_key, ciphertext); } else /* 1 - 9 */ { byte_sub(ciphertext, intermediatea); shift_row(intermediatea, intermediateb); mix_column(&intermediateb[0], &intermediatea[0]); mix_column(&intermediateb[4], &intermediatea[4]); mix_column(&intermediateb[8], &intermediatea[8]); mix_column(&intermediateb[12], &intermediatea[12]); xor_128(intermediatea, round_key, ciphertext); next_key(round_key, round); } } } /****************************************/ /* CRC32() */ /* Calculates the CRC32 of a sequence */ /* of octets. */ /****************************************/ void crc32(unsigned char *crc, unsigned char *data, int length) { int i; int index; unsigned char ch; unsigned char table_entry[4]; crc[3] = 0xff; crc[2] = 0xff; crc[1] = 0xff; crc[0] = 0xff; for (i=0; i> 2) & 0x03); frame_subtype = ((fc0 >> 4) & 0x0f); from_ds = (fc1 & 0x2) >> 1; to_ds = (fc1 & 0x1); *a4_exists = (from_ds & to_ds); *qc_exists = ( (frame_subtype == 0x08) /* Assumed QoS subtypes */ || (frame_subtype == 0x09) /* Likely to change. */ || (frame_subtype == 0x0a) || (frame_subtype == 0x0b) ); duration = mpdu[2] + (mpdu[3] * 256); seq_control = mpdu[22] + (mpdu[23] * 256); if (*qc_exists) { if (*a4_exists) { qos_control = mpdu[30] + (mpdu[31] * 256); } else { qos_control = mpdu[24] + (mpdu[25] * 256); } } if (frame_type != DATA_FRAME) { printf ("fc0 = %x\n", fc0); printf ("ERROR, frame_type %d != DATA_FRAME\n",frame_type); exit(1); } #ifdef SHOW_HEADER_FIELDS printf("---- fc = 0x%x\n",fc); printf("---- Dur = 0x%x\n", duration); printf("---- A1 = (lsb) %02x-%02x-%02x-%02x-%02x-%02x (msb)\n", mpdu[4], mpdu[5], mpdu[6], mpdu[7], mpdu[8], mpdu[9] ); printf("---- A2 = (lsb) %02x-%02x-%02x-%02x-%02x-%02x (msb)\n", mpdu[10], mpdu[11], mpdu[12], mpdu[13], mpdu[14], mpdu[15] ); printf("---- A3 = (lsb) %02x-%02x-%02x-%02x-%02x-%02x (msb)\n", mpdu[16], mpdu[17], mpdu[18], mpdu[19], mpdu[20], mpdu[21] ); printf("---- SC = 0x%04x\n", seq_control ); #endif /* Calculate header length, */ /* taking A4 and QoS_Control */ /* fields into account if necessary */ *header_length = 24; if (*a4_exists) { *header_length += 6; #ifdef SHOW_HEADER_FIELDS printf("---- a4 field present\n"); printf("---- A4 = (lsb) %02x-%02x-%02x-%02x-%02x-%02x (msb)\n", mpdu[24], mpdu[25], mpdu[26], mpdu[27], mpdu[28], mpdu[29] ); #endif } if (*qc_exists) { *header_length +=2; #ifdef SHOW_HEADER_FIELDS printf("---- QoS Control field exists\n"); printf("---- QC = 0x%04x\n", qos_control ); #endif } /* Payload is (total_length - header), CRC not included here */ *payload_length = length - *header_length; #ifdef SHOW_HEADER_FIELDS printf("---- Length = %d octets\n",length); printf("---- Header Length = %d octets\n", *header_length); printf("---- Payload Length = %d octets\n", *payload_length); if (fc1 & 0x40) /* If protected bit on, show def key ID */ { def_key_id = (int)(mpdu[*header_length + 3] >> 6); printf("---- DefKeyId = %d\n",def_key_id); } #endif #ifdef SHOW_HEADER printf("---- Header = "); for (i=0; i<(16); i++) /* First sixteen bytes of header */ { printf("%02x ",mpdu[i]); } printf("\n"); printf("---- Header = "); for (i=0; i<(*header_length - 16); i++) /* Remainder of header */ { printf("%02x ",mpdu[i+16]); } printf("\n"); #endif if (fc1 & 0x40) /* If protected bit set, the display PN, data and MIC */ { #ifdef SHOW_RSN_HEADER printf("---- RSN HEADER = "); for (i=0; i<8; i++) { printf("%02x ",mpdu[i+ *header_length]); } printf("\n"); #endif num_blocks = (*payload_length - 16) / 16; payload_remainder = (*payload_length - 16) % 16; #ifdef SHOW_DATA for (i=0;i 0) { printf("---- DATA = "); for (j=0; j 0) { printf("---- DATA = "); for (j=0; j 0) { for (j = 0; j < 16; j++) padded_buffer[j] = 0x00; for (j = 0; j < payload_remainder; j++) { padded_buffer[j] = plaintext[payload_index++]; } bitwise_xor(aes_out, padded_buffer, chain_buffer); aes128k128d(key, chain_buffer, aes_out); } /* aes_out contains padded mic, discard most significant */ /* 8 bytes to generate 64 bit MIC */ for (j = 0 ; j < 8; j++) mic[j] = aes_out[j]; /* Insert MIC into payload */ for (j = 0; j < 8; j++) { //printf(" mic putting index %d, value = 0x%02x\n",(payload_index + j), mic[j]); ciphertext[payload_index + j] = mic[j]; } length += 8; *payload_length += 8; /* Insert RSN header */ for (i=0;i < *payload_length; i++) { ciphertext[length+7-i] = ciphertext[length-1-i]; //printf (" moving %d to %d , value = 0x%02x\n",(length-1-i),(length+7-i), ciphertext[length+7-i]); } ciphertext[*header_length] = pn_vector[0]; ciphertext[1+ *header_length] = pn_vector[1]; ciphertext[2+ *header_length] = 0x00; /* Reserved byte */ if (def_key_id==0) ciphertext[3+ *header_length] = 0x20; /* EIV bit set, defkeyID=0 */ if (def_key_id==1) ciphertext[3+ *header_length] = 0x60; /* EIV bit set, defkeyID=1 */ if (def_key_id==2) ciphertext[3+ *header_length] = 0xa0; /* EIV bit set, defkeyID=2 */ if (def_key_id==3) ciphertext[3+ *header_length] = 0xe0; /* EIV bit set, defkeyID=3 */ ciphertext[4+ *header_length] = pn_vector[2]; ciphertext[5+ *header_length] = pn_vector[3]; ciphertext[6+ *header_length] = pn_vector[4]; ciphertext[7+ *header_length] = pn_vector[5]; length += 8; *payload_length +=8; /* Force the protected bit on */ ciphertext[1] = ciphertext[1] | 0x40; /**** Encrypt the payload and MIC ****/ payload_index = *header_length + 8; /* header + RSN header */ for (i=0; i< num_blocks; i++) { construct_ctr_preload( ctr_preload, a4_exists, qc_exists, plaintext, pn_vector, i+1 ); #ifdef SHOW_CTR_PRELOAD printf("---- CTR_PRELOAD(%i) = (lsb) %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x (msb)\n", i+1, ctr_preload[0],ctr_preload[1],ctr_preload[2],ctr_preload[3], ctr_preload[4],ctr_preload[5],ctr_preload[6],ctr_preload[7], ctr_preload[8],ctr_preload[9],ctr_preload[10],ctr_preload[11], ctr_preload[12],ctr_preload[13],ctr_preload[14],ctr_preload[15]); #endif aes128k128d(key, ctr_preload, aes_out); bitwise_xor(aes_out, &ciphertext[payload_index], chain_buffer); for (j=0; j<16;j++) ciphertext[payload_index++] = chain_buffer[j]; } if (payload_remainder > 0) /* If there is a short final block, then pad it,*/ { /* encrypt it and copy the unpadded part back */ construct_ctr_preload( ctr_preload, a4_exists, qc_exists, plaintext, pn_vector, num_blocks+1 ); for (j = 0; j < 16; j++) padded_buffer[j] = 0x00; for (j = 0; j < payload_remainder; j++) { padded_buffer[j] = ciphertext[payload_index+j]; } aes128k128d(key, ctr_preload, aes_out); bitwise_xor(aes_out, padded_buffer, chain_buffer); for (j=0; j 0) { printf("\t\t"); for (j=0; j 0) /* Print the final line */ { printf("\t\t"); for (j=0; j