rc4.c (116174) | rc4.c (186179) |
---|---|
1/* 2 * rc4.c 3 * 4 * Copyright (c) 1996-2000 Whistle Communications, Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or --- 21 unchanged lines hidden (view full) --- 30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 34 * OF SUCH DAMAGE. 35 */ 36 37#include <sys/cdefs.h> | 1/* 2 * rc4.c 3 * 4 * Copyright (c) 1996-2000 Whistle Communications, Inc. 5 * All rights reserved. 6 * 7 * Subject to the following obligations and disclaimer of warranty, use and 8 * redistribution of this software, in source or object code forms, with or --- 21 unchanged lines hidden (view full) --- 30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 34 * OF SUCH DAMAGE. 35 */ 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/sys/crypto/rc4/rc4.c 116174 2003-06-10 21:44:29Z obrien $"); | 38__FBSDID("$FreeBSD: head/sys/crypto/rc4/rc4.c 186179 2008-12-16 13:58:37Z mav $"); |
39 40#include <sys/param.h> 41#include <sys/kernel.h> 42#include <sys/module.h> 43#include <sys/types.h> 44#include <crypto/rc4/rc4.h> 45 46static __inline void --- 9 unchanged lines hidden (view full) --- 56/* 57 * Initialize an RC4 state buffer using the supplied key, 58 * which can have arbitrary length. 59 */ 60void 61rc4_init(struct rc4_state *const state, const u_char *key, int keylen) 62{ 63 u_char j; | 39 40#include <sys/param.h> 41#include <sys/kernel.h> 42#include <sys/module.h> 43#include <sys/types.h> 44#include <crypto/rc4/rc4.h> 45 46static __inline void --- 9 unchanged lines hidden (view full) --- 56/* 57 * Initialize an RC4 state buffer using the supplied key, 58 * which can have arbitrary length. 59 */ 60void 61rc4_init(struct rc4_state *const state, const u_char *key, int keylen) 62{ 63 u_char j; |
64 int i; | 64 int i, k; |
65 66 /* Initialize state with identity permutation */ 67 for (i = 0; i < 256; i++) 68 state->perm[i] = (u_char)i; 69 state->index1 = 0; 70 state->index2 = 0; 71 72 /* Randomize the permutation using key data */ | 65 66 /* Initialize state with identity permutation */ 67 for (i = 0; i < 256; i++) 68 state->perm[i] = (u_char)i; 69 state->index1 = 0; 70 state->index2 = 0; 71 72 /* Randomize the permutation using key data */ |
73 for (j = i = 0; i < 256; i++) { 74 j += state->perm[i] + key[i % keylen]; | 73 for (j = i = k = 0; i < 256; i++) { 74 j += state->perm[i] + key[k]; |
75 swap_bytes(&state->perm[i], &state->perm[j]); | 75 swap_bytes(&state->perm[i], &state->perm[j]); |
76 if (++k >= keylen) 77 k = 0; |
|
76 } 77} 78 79/* 80 * Encrypt some data using the supplied RC4 state buffer. 81 * The input and output buffers may be the same buffer. 82 * Since RC4 is a stream cypher, this function is used 83 * for both encryption and decryption. --- 43 unchanged lines hidden --- | 78 } 79} 80 81/* 82 * Encrypt some data using the supplied RC4 state buffer. 83 * The input and output buffers may be the same buffer. 84 * Since RC4 is a stream cypher, this function is used 85 * for both encryption and decryption. --- 43 unchanged lines hidden --- |