summaryrefslogtreecommitdiff
path: root/arcfour.c
diff options
context:
space:
mode:
authorNiels Möller <nisse@lysator.liu.se>1998-12-21 02:44:11 +0100
committerNiels Möller <nisse@lysator.liu.se>1998-12-21 02:44:11 +0100
commit9f77bb3afc3b3684be0c90db144c2af56a2f56e4 (patch)
tree258518edcda4a653b83ab32f582f0d164eef31d7 /arcfour.c
parent0ce1de9e529e34d5e38f7b7f4e5fcc27b02c44b5 (diff)
downloadnettle-9f77bb3afc3b3684be0c90db144c2af56a2f56e4.tar.gz
Renamed rc4 -> arcfour.
Rev: src/symmetric/Makefile.am.in:1.6 Rev: src/symmetric/arcfour.c:1.1 Rev: src/symmetric/include/arcfour.h:1.1 Rev: src/symmetric/include/rc4.h:1.5(DEAD) Rev: src/symmetric/rc4.c:1.3(DEAD)
Diffstat (limited to 'arcfour.c')
-rw-r--r--arcfour.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/arcfour.c b/arcfour.c
new file mode 100644
index 00000000..4aeedc9d
--- /dev/null
+++ b/arcfour.c
@@ -0,0 +1,49 @@
+/* arcfour.c
+ *
+ */
+
+#include "crypto_types.h"
+#include <arcfour.h>
+
+#ifdef RCSID
+RCSID("$Id$");
+#endif
+
+#define SWAP(a,b) do { int _t = a; a = b; b = _t; } while(0)
+
+void arcfour_set_key(struct arcfour_ctx *ctx, const UINT8 *key, UINT32 len)
+{
+ register UINT8 j; /* Depends on the eight-bitness of these variables. */
+ unsigned i;
+ UINT32 k;
+
+ /* Initialize context */
+ i = 0;
+ do ctx->S[i] = i; while (++i < 256);
+
+ /* Expand key */
+ 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 < 256);
+
+ ctx->i = ctx->j = 0;
+}
+
+void arcfour_crypt(struct arcfour_ctx *ctx, UINT8 *dest,
+ const UINT8 *src, UINT32 len)
+{
+ register UINT8 i, j;
+
+ i = ctx->i; j = ctx->j;
+ while(len--)
+ {
+ i++; i &= 0xff;
+ j += ctx->S[i]; j &= 0xff;
+ SWAP(ctx->S[i], ctx->S[j]);
+ *dest++ = *src++ ^ ctx->S[ (ctx->S[i] + ctx->S[j]) & 0xff ];
+ }
+ ctx->i = i; ctx->j = j;
+}