From fe9e31e7a3bcb6d9635837cb3a642234a7fa3203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se> Date: Tue, 11 Mar 1997 04:11:59 +0100 Subject: [PATCH] Bugfixes Rev: src/modules/_Crypto/lib/rc4.c:1.3 --- src/modules/_Crypto/lib/rc4.c | 41 ++++++++++++++--------------------- 1 file changed, 16 insertions(+), 25 deletions(-) diff --git a/src/modules/_Crypto/lib/rc4.c b/src/modules/_Crypto/lib/rc4.c index 0c6603b360..be512f0cc3 100644 --- a/src/modules/_Crypto/lib/rc4.c +++ b/src/modules/_Crypto/lib/rc4.c @@ -5,48 +5,39 @@ #include "crypto_types.h" #include <rc4.h> -#define SWAP(a,b) (a ^= b, b ^= a, a ^= b) - -#if 0 -void rc4_init(struct rc4_ctx *ctx) -{ - int i; - for (i = 0; i < 256; i++) - ctx->S[i] = i; - ctx->i = ctx->j = 0; -} -#endif +#define SWAP(a,b) do { int _t = a; a = b; b = _t; } while(0) void rc4_set_key(struct rc4_ctx *ctx, const unsigned INT8 *key, INT32 len) { - register unsigned i, j; + register unsigned INT8 i, j; /* Depends on the eight-bitness of these variables. */ INT32 k; /* Initialize context */ - for (i = 0; i < 256; i++) - ctx->S[i] = i; + i = 0; + do ctx->S[i] = i; while (++i); /* Expand key */ - for (i = j = k = 0; i < 256; i++) - { - j += ctx->S[i] + key[k]; - SWAP(ctx->S[i], ctx->S[j]); - k = (k+1) % len; /* Repeat key if needed */ - } - ctx->j = j; ctx->i = 0; + i = j = k = 0; + do { + j += ctx->S[i] + key[k]; + SWAP(ctx->S[i], ctx->S[j]); + k = (k+1) % len; /* Repeat key if needed */ + } while(++i); + + ctx->i = ctx->j = 0; } void rc4_crypt(struct rc4_ctx *ctx, unsigned INT8 *dest, const unsigned INT8 *src, INT32 len) { - register unsigned INT8 i,j; /* Depends on the 8-bitness of these variables */ + register unsigned INT8 i, j; /* Depends on the eight-bitness of these variables */ i = ctx->i; j = ctx->j; while(len--) { - i = (i + 1); - j = (j + ctx->S[i]); + i++; + j += ctx->S[i]; SWAP(ctx->S[i], ctx->S[j]); - *dest++ = *src++ ^ ctx->S[ (ctx->S[i] + ctx->S[j]) ]; + *dest++ = *src++ ^ ctx->S[ (ctx->S[i] + ctx->S[j]) & 0xff ]; } ctx->i = i; ctx->j = j; } -- GitLab