Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Dec 2008 13:58:37 +0000 (UTC)
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r186179 - head/sys/crypto/rc4
Message-ID:  <200812161358.mBGDwbL5039244@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: mav
Date: Tue Dec 16 13:58:37 2008
New Revision: 186179
URL: http://svn.freebsd.org/changeset/base/186179

Log:
  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.

Modified:
  head/sys/crypto/rc4/rc4.c

Modified: head/sys/crypto/rc4/rc4.c
==============================================================================
--- head/sys/crypto/rc4/rc4.c	Tue Dec 16 11:06:30 2008	(r186178)
+++ head/sys/crypto/rc4/rc4.c	Tue Dec 16 13:58:37 2008	(r186179)
@@ -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, 
 	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;
 	}
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200812161358.mBGDwbL5039244>