2010-09-26 21:54:31 -06:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "arc4.h"
|
|
|
|
|
|
|
|
#define swap(a, b) do {int _swap=a; a=b, b=_swap;} while (0)
|
|
|
|
|
|
|
|
void
|
|
|
|
arc4_init(struct arc4_ctx *ctx, uint8_t const *key, size_t keylen)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int j = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i += 1) {
|
|
|
|
ctx->S[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i += 1) {
|
|
|
|
j = (j + ctx->S[i] + key[i % keylen]) % 256;
|
|
|
|
swap(ctx->S[i], ctx->S[j]);
|
|
|
|
}
|
|
|
|
ctx->i = 0;
|
|
|
|
ctx->j = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2011-01-21 23:01:30 -07:00
|
|
|
arc4_out(struct arc4_ctx *ctx)
|
2010-09-26 21:54:31 -06:00
|
|
|
{
|
|
|
|
ctx->i = (ctx->i + 1) % 256;
|
|
|
|
ctx->j = (ctx->j + ctx->S[ctx->i]) % 256;
|
|
|
|
swap(ctx->S[ctx->i], ctx->S[ctx->j]);
|
|
|
|
return ctx->S[(ctx->S[ctx->i] + ctx->S[ctx->j]) % 256];
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
arc4_crypt(struct arc4_ctx *ctx,
|
2011-01-21 23:01:30 -07:00
|
|
|
uint8_t *obuf, const uint8_t *ibuf, size_t buflen)
|
2010-09-26 21:54:31 -06:00
|
|
|
{
|
|
|
|
size_t k;
|
|
|
|
|
|
|
|
for (k = 0; k < buflen; k += 1) {
|
2011-01-21 23:01:30 -07:00
|
|
|
obuf[k] = ibuf[k] ^ arc4_out(ctx);
|
2010-09-26 21:54:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-21 23:01:30 -07:00
|
|
|
arc4_crypt_buffer(const uint8_t *key, size_t keylen,
|
2010-09-26 21:54:31 -06:00
|
|
|
uint8_t *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
struct arc4_ctx ctx;
|
|
|
|
|
|
|
|
arc4_init(&ctx, key, keylen);
|
|
|
|
arc4_crypt(&ctx, buf, buf, buflen);
|
|
|
|
}
|
2011-01-19 17:21:07 -07:00
|
|
|
|
2011-02-03 07:31:42 -07:00
|
|
|
|
|
|
|
#ifdef ARC4_MAIN
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct arc4_ctx ctx;
|
|
|
|
|
|
|
|
/* Read key and initialize context */
|
|
|
|
{
|
|
|
|
uint8_t key[256];
|
|
|
|
size_t keylen = 0;
|
|
|
|
char *ekey = getenv("KEY");
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
if (! (f = fopen(argv[1], "r"))) {
|
|
|
|
perror(argv[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f = fdopen(3, "r");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
keylen = fread(key, 1, sizeof(key), f);
|
|
|
|
fclose(f);
|
|
|
|
} else if (ekey) {
|
|
|
|
keylen = strlen(ekey);
|
|
|
|
if (keylen > sizeof(key)) {
|
|
|
|
keylen = sizeof(key);
|
|
|
|
}
|
|
|
|
memcpy(key, ekey, keylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == keylen) {
|
|
|
|
fprintf(stderr, "Usage: %s [KEYFILE] <PLAINTEXT\n", argv[0]);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "You can also pass in the key on fd 3 or in\n");
|
|
|
|
fprintf(stderr, "$KEY; omit KEYFILE in this case.\n");
|
|
|
|
return EX_IOERR;
|
|
|
|
}
|
|
|
|
arc4_init(&ctx, key, (size_t)keylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encrypt */
|
|
|
|
while (1) {
|
|
|
|
int c = getchar();
|
|
|
|
|
|
|
|
if (EOF == c) break;
|
|
|
|
putchar(c ^ arc4_out(&ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ARC4_MAIN */
|