/* * Is a guessed key correct? */ static int correct(PTW_attackstate * state, unsigned char * key, int keylen) { int i; int j; int k; unsigned char keybuf[PTW_KSBYTES]; rc4state rc4state; // We need at least 3 sessions to be somehow certain if (state->sessions_collected < 3) { return 0; } tried++; k = rand()%(state->sessions_collected-10); for ( i=k; i < k+10; i++) { memcpy(&keybuf[IVBYTES], key, keylen); memcpy(keybuf, state->sessions[i].iv, IVBYTES); rc4init(keybuf, keylen+IVBYTES, &rc4state); for (j = 0; j < TESTBYTES; j++) { if ((rc4update(&rc4state) ^ state->sessions[i].keystream[j]) != 0) { return 0; } } } return 1; }
void addRound(uint8_t * key, PTW_attackstate * state) { int j; uint8_t iv[3]; uint8_t ks[KEYBYTES]; rc4state rc4s; for (j = 0; j < IVBYTES; j++) { key[j] = rand()%n; iv[j] = key[j]; } rc4init(key, KEYBYTES, &rc4s); for (j = 0; j < KEYBYTES; j++) { ks[j] = rc4update(&rc4s); } PTW_addsession(state, iv, ks); return; }
/* * Is a guessed key correct? */ static int correct(PTW_attackstate * state, uint8_t * key, int keylen) { int i; int j; uint8_t keybuf[PTW_KSBYTES]; rc4state rc4state; for (i = 0; i < state->sessions_collected; i++) { memcpy(&keybuf[IVBYTES], key, keylen); memcpy(keybuf, state->sessions[i].iv, IVBYTES); rc4init(keybuf, keylen+IVBYTES, &rc4state); for (j = 0; j < TESTBYTES; j++) { if ((rc4update(&rc4state) ^ state->sessions[i].keystream[j]) != 0) { return 0; } } } return 1; }
/* main program */ int main() { RC4STATUS *s = new_rc4status(RC4MOD); char texto[] = "Arriba cachipurriana"; size_t lon = strlen(texto); char clave[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08, 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17, 0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18, }; fprintbuf(stdout, lon, (unsigned char *)texto, -1, -1, "Texto a cifrar"); fprintf(stdout, "Clave nula (256 NULL's)\n"); rc4init(s, NULL, 0); /* key is 0's */ rc4cipher(s, (RC4 *)texto, lon); fprintbuf(stdout, lon, (unsigned char *)texto, -1, -1, "Texto Cifrado"); rc4init(s, NULL, 0); rc4uncipher(s, (RC4 *)texto, lon); fprintbuf(stdout, lon, (unsigned char *)texto, -1, -1, "Texto descifrado"); fprintbuf(stdout, sizeof clave, (unsigned char *)clave, -1, -1, "Clave de cifrado:"); rc4init(s, (RC4 *)clave, sizeof clave); rc4cipher(s, (RC4 *)texto, lon); fprintbuf(stdout, lon, (unsigned char *)texto, -1, -1, "Texto Cifrado"); rc4init(s, (RC4 *)clave, sizeof clave); rc4uncipher(s, (RC4 *)texto, lon); fprintbuf(stdout, lon, (unsigned char *)texto, -1, -1, "Texto descifrado"); } /* main */