/** Do 40-bit rc4-decrypt with key on bs of length len and put the result in out */ static void rc4Decrypt40b(const uint8_t *key, const uint8_t *bs, const unsigned int len, uint8_t *out) { uint8_t state[256]; register unsigned int i; register uint8_t j, tmp; /** initialize the state */ memcpy(state, initial_state, 256); /** do the shuffle */ j = key[0]; state[0] = j; state[j] = 0; i = 0; do { key_pass(1); key_pass(2); key_pass(3); key_pass(4); key_pass(0); } while(i < 255); j = 0; for(i=1;i<=len;++i) { tmp = state[i]; j += tmp; state[i] = state[j]; state[j] = tmp; tmp += state[i]; out[i-1] = bs[i-1]^state[tmp]; } }
static void rc4DecryptArb(const uint8_t *key, const uint8_t *bs, const unsigned int len, uint8_t *out) { uint8_t state[256]; register unsigned int i; register uint8_t j, tmp; /** initialize the state */ memcpy(state, initial_state, 256); /** do the shuffle */ j = 0; i = -1; do { key_pass( (i % keyLen) ); } while(i < 255); j = 0; for(i=1;(unsigned int)i<=len;++i) { tmp = state[i]; j += tmp; state[i] = state[j]; state[j] = tmp; tmp += state[i]; out[i-1] = bs[i-1]^state[tmp]; } }
/** Do rc4-decrypt with key on bs of length 32 and compare it to match */ __attribute__ ((pure)) bool rc4Match40b(const uint8_t *key, const uint8_t *bs,const uint8_t *match) { uint8_t state[256]; register unsigned int i; register uint8_t j, tmp; /** initialize the state */ memcpy(state, initial_state, 256); /** do the shuffle */ j = key[0]; state[0] = j; state[j] = 0; i = 0; do { key_pass(1); key_pass(2); key_pass(3); key_pass(4); key_pass(0); } while(i < 255); j = 0; for(i=1;i<=32;++i) { tmp = state[i]; j += tmp; state[i] = state[j]; state[j] = tmp; /** Only continue if we match the match-strings characters. The match should only happen once every 256 try or so and that is the motivation behind the likely-hint */ tmp += state[i]; if(likely((bs[i-1]^state[tmp]) != match[i-1])) return false; } return true; }
/** Do 128-bit rc4-decrypt with key on bs of length len and put the result in out */ static void rc4Decrypt128b(const uint8_t *key, const uint8_t *bs, const unsigned int len, uint8_t *out) { uint8_t state[256]; register int i; register uint8_t j, tmp; assert(len < 256); /** initialize the state */ memcpy(state, initial_state, 256); /** do the shuffle */ j = 0; i = -1; do { key_pass( 0); key_pass( 1); key_pass( 2); key_pass( 3); key_pass( 4); key_pass( 5); key_pass( 6); key_pass( 7); key_pass( 8); key_pass( 9); key_pass(10); key_pass(11); key_pass(12); key_pass(13); key_pass(14); key_pass(15); } while(i < 255); j = 0; for(i=1;(unsigned int)i<=len;++i) { tmp = state[i]; j += tmp; state[i] = state[j]; state[j] = tmp; tmp += state[i]; out[i-1] = bs[i-1]^state[tmp]; } }