diff options
author | mav <mav@FreeBSD.org> | 2008-12-16 13:58:37 +0000 |
---|---|---|
committer | mav <mav@FreeBSD.org> | 2008-12-16 13:58:37 +0000 |
commit | f702951b49e8263ed5b1962583adfa984f602e83 (patch) | |
tree | a1f31956f2edea0480ef1cfc3e6495545975c2ec /sys/crypto/rc4/rc4.c | |
parent | bf1d63fc616b975902da656f50daa59075203bb0 (diff) | |
download | FreeBSD-src-f702951b49e8263ed5b1962583adfa984f602e83.zip FreeBSD-src-f702951b49e8263ed5b1962583adfa984f602e83.tar.gz |
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.
Diffstat (limited to 'sys/crypto/rc4/rc4.c')
-rw-r--r-- | sys/crypto/rc4/rc4.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/sys/crypto/rc4/rc4.c b/sys/crypto/rc4/rc4.c index f94a554..dbdbd7a 100644 --- a/sys/crypto/rc4/rc4.c +++ b/sys/crypto/rc4/rc4.c @@ -61,7 +61,7 @@ void rc4_init(struct rc4_state *const state, const u_char *key, int keylen) { u_char j; - int i; + int i, k; /* Initialize state with identity permutation */ 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; /* Randomize the permutation using key data */ - for (j = i = 0; i < 256; i++) { - j += state->perm[i] + key[i % keylen]; + for (j = i = k = 0; i < 256; i++) { + j += state->perm[i] + key[k]; swap_bytes(&state->perm[i], &state->perm[j]); + if (++k >= keylen) + k = 0; } } |