static void rc4(rc4_key* key, unsigned char *buffer_ptr, int buffer_len) { unsigned char x; unsigned char y; unsigned char* state; unsigned char xorIndex; short counter; x = key->x; y = key->y; state = &key->state[0]; for(counter = 0; counter < buffer_len; counter ++) { x = (unsigned char)((x + 1) % 256); y = (unsigned char)((state[x] + y) % 256); ARES_SWAP_BYTE(&state[x], &state[y]); xorIndex = (unsigned char)((state[x] + state[y]) % 256); buffer_ptr[counter] = (unsigned char)(buffer_ptr[counter]^state[xorIndex]); } key->x = x; key->y = y; }
static int init_id_key(rc4_key* key,int key_data_len) { unsigned char index1; unsigned char index2; unsigned char* state; short counter; unsigned char *key_data_ptr = 0; key_data_ptr = calloc(1,key_data_len); if (!key_data_ptr) return ARES_ENOMEM; state = &key->state[0]; for(counter = 0; counter < 256; counter++) /* unnecessary AND but it keeps some compilers happier */ state[counter] = (unsigned char)(counter & 0xff); randomize_key(key->state,key_data_len); key->x = 0; key->y = 0; index1 = 0; index2 = 0; for(counter = 0; counter < 256; counter++) { index2 = (unsigned char)((key_data_ptr[index1] + state[counter] + index2) % 256); ARES_SWAP_BYTE(&state[counter], &state[index2]); index1 = (unsigned char)((index1 + 1) % key_data_len); } free(key_data_ptr); return ARES_SUCCESS; }