Avoid 256 integer divisions per rc4_init() call. Replace it with using
separate index variable. It gives more then double rc4_init() performance increase on tested i386 P4. It also gives about 15% speedup to PPTP VPN with stateless MPPE encryption (by ng_mppc) which calls rc4_init() for every packet.
This commit is contained in:
parent
848552f31f
commit
f2831a952d
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=186179
@ -61,7 +61,7 @@ void
|
|||||||
rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
|
rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
|
||||||
{
|
{
|
||||||
u_char j;
|
u_char j;
|
||||||
int i;
|
int i, k;
|
||||||
|
|
||||||
/* Initialize state with identity permutation */
|
/* Initialize state with identity permutation */
|
||||||
for (i = 0; i < 256; i++)
|
for (i = 0; i < 256; i++)
|
||||||
@ -70,9 +70,11 @@ rc4_init(struct rc4_state *const state, const u_char *key, int keylen)
|
|||||||
state->index2 = 0;
|
state->index2 = 0;
|
||||||
|
|
||||||
/* Randomize the permutation using key data */
|
/* Randomize the permutation using key data */
|
||||||
for (j = i = 0; i < 256; i++) {
|
for (j = i = k = 0; i < 256; i++) {
|
||||||
j += state->perm[i] + key[i % keylen];
|
j += state->perm[i] + key[k];
|
||||||
swap_bytes(&state->perm[i], &state->perm[j]);
|
swap_bytes(&state->perm[i], &state->perm[j]);
|
||||||
|
if (++k >= keylen)
|
||||||
|
k = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user