Skip to content
Snippets Groups Projects
Commit fe9e31e7 authored by Niels Möller's avatar Niels Möller
Browse files

Bugfixes

Rev: src/modules/_Crypto/lib/rc4.c:1.3
parent a42e99a0
No related branches found
No related tags found
No related merge requests found
......@@ -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++)
{
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 */
}
ctx->j = j; ctx->i = 0;
} 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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment