Gussy up arc4.c

This commit is contained in:
Neale Pickett 2011-03-18 19:21:45 -06:00
parent 64f7754800
commit 5dbf9e8adb
8 changed files with 157 additions and 314 deletions

View File

@ -1,10 +1,8 @@
TARGETS = bubblebabble arc4 tea TARGETS = bubblebabble arc4
all: $(TARGETS) all: $(TARGETS)
arc4: CFLAGS=-DARC4_MAIN arc4: CFLAGS=-DARC4_MAIN
tea: tea.o xxtea.o
clean: clean:
rm -f *.o $(TARGETS) rm -f *.o $(TARGETS)

View File

@ -1,15 +1,9 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "arc4.h" #include "arc4.h"
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v)
#define swap(a, b) do {uint8_t _swap=a; a=b, b=_swap;} while (0) #define swap(a, b) do {uint8_t _swap=a; a=b, b=_swap;} while (0)
void void
@ -50,16 +44,6 @@ arc4_crypt(struct arc4_ctx *ctx,
} }
} }
void
arc4_crypt_buffer(const uint8_t *key, size_t keylen,
uint8_t *buf, size_t buflen)
{
struct arc4_ctx ctx;
arc4_init(&ctx, key, keylen);
arc4_crypt(&ctx, buf, buf, buflen);
}
/* Create a nonce as an arc4 stream with key=seed */ /* Create a nonce as an arc4 stream with key=seed */
void void
arc4_nonce(uint8_t *nonce, size_t noncelen, arc4_nonce(uint8_t *nonce, size_t noncelen,
@ -75,24 +59,135 @@ arc4_nonce(uint8_t *nonce, size_t noncelen,
} }
#ifdef ARC4_MAIN /***************************************************
*
* Psuedo Random Number Generation
*
*/
static struct arc4_ctx prng_ctx;
static int prng_initialized = 0;
#include <stdio.h> void
#include <sysexits.h> arc4_rand_seed(const uint8_t *seed, size_t seedlen)
#include <time.h> {
#include <string.h> arc4_init(&prng_ctx, seed, seedlen);
#include <sys/types.h> prng_initialized = 1;
#include <unistd.h> }
static void
arc4_rand_autoseed()
{
if (! prng_initialized) {
uint8_t key[ARC4_KEYLEN];
FILE *urandom;
/* Open /dev/urandom or die trying */
urandom = fopen("/dev/urandom", "r");
if (! urandom) {
perror("Opening /dev/urandom");
abort();
}
setbuf(urandom, NULL);
fread(&key, sizeof(key), 1, urandom);
fclose(urandom);
arc4_rand_seed(key, sizeof(key));
}
}
uint8_t
arc4_rand8()
{
arc4_rand_autoseed();
return arc4_out(&prng_ctx);
}
uint32_t
arc4_rand32()
{
arc4_rand_autoseed();
return ((arc4_out(&prng_ctx) << 0) |
(arc4_out(&prng_ctx) << 8) |
(arc4_out(&prng_ctx) << 16) |
(arc4_out(&prng_ctx) << 24));
}
/*****************************************
*
* Stream operations
*
*/
ssize_t
arc4_encrypt_stream(FILE *out, FILE *in,
const uint8_t *key, size_t keylen)
{
struct arc4_ctx ctx;
uint32_t seed = arc4_rand32();
uint8_t nonce[ARC4_KEYLEN];
ssize_t written = 0;
int i;
fwrite("arc4", 4, 1, out);
fwrite(&seed, sizeof(seed), 1, out);
arc4_nonce(nonce, sizeof(nonce), &seed, sizeof(seed));
for (i = 0; i < keylen; i += 1) {
nonce[i] ^= key[i];
}
arc4_init(&ctx, nonce, sizeof(nonce));
while (1) {
int c = fgetc(in);
if (EOF == c) break;
fputc((uint8_t)c ^ arc4_out(&ctx), out);
written += 1;
}
return written;
}
int int
usage(const char *prog) arc4_decrypt_stream(FILE *out, FILE *in,
const uint8_t *key, size_t keylen)
{ {
fprintf(stderr, "Usage: %s [-e] <PLAINTEXT\n", prog); struct arc4_ctx ctx;
fprintf(stderr, "\n"); uint32_t seed;
fprintf(stderr, "You must pass in a key on fd 3 or in the environment variable KEY.\n"); uint8_t nonce[ARC4_KEYLEN];
return EX_USAGE; ssize_t written = 0;
char sig[4];
int i;
fread(&sig, sizeof(sig), 1, stdin);
if (memcmp(sig, "arc4", 4)) {
return -1;
}
fread(&seed, sizeof(seed), 1, stdin);
arc4_nonce(nonce, sizeof(nonce), &seed, sizeof(seed));
for (i = 0; i < keylen; i += 1) {
nonce[i] ^= key[i];
}
arc4_init(&ctx, nonce, sizeof(nonce));
while (1) {
int c = fgetc(in);
if (EOF == c) break;
fputc((uint8_t)c ^ arc4_out(&ctx), out);
written += 1;
}
return written;
} }
#ifdef ARC4_MAIN
#include <sysexits.h>
#include <unistd.h>
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -100,7 +195,6 @@ main(int argc, char *argv[])
uint8_t key[ARC4_KEYLEN] = {0}; uint8_t key[ARC4_KEYLEN] = {0};
size_t keylen; size_t keylen;
uint8_t nonce[ARC4_KEYLEN]; uint8_t nonce[ARC4_KEYLEN];
time_t seed;
int i; int i;
/* Read key and initialize context */ /* Read key and initialize context */
@ -111,48 +205,25 @@ main(int argc, char *argv[])
keylen = strlen(ekey); keylen = strlen(ekey);
memcpy(key, ekey, keylen); memcpy(key, ekey, keylen);
} else { } else {
FILE *f = fdopen(3, "r"); keylen = read(3, key, sizeof(key));
if (NULL == f) {
return usage(argv[0]);
}
keylen = fread(key, 1, ARC4_KEYLEN, f);
fclose(f);
} }
} }
if (argv[1] && (0 == strcmp(argv[1], "-e"))) { if (! argv[1]) {
seed = time(NULL) * getpid(); if (-1 == arc4_decrypt_stream(stdout, stdin, key, keylen)) {
fwrite("arc4", 1, 4, stdout); perror("decrypting");
fwrite(&seed, sizeof(seed), 1, stdout);
} else if (argv[1]) {
return usage(argv[0]);
} else {
char sig[4];
fread(&sig, sizeof(sig), 1, stdin);
if (memcmp(sig, "arc4", 4)) {
fprintf(stderr, "%s: error: Input is not arc4-encrypted.", argv[0]);
return 1; return 1;
} }
fread(&seed, sizeof(seed), 1, stdin); } else if (0 == strcmp(argv[1], "-e")) {
if (-1 == arc4_encrypt_stream(stdout, stdin, key, keylen)) {
perror("encrypting");
return 1;
} }
} else {
arc4_nonce(nonce, sizeof(nonce), &seed, sizeof(seed)); fprintf(stderr, "Usage: %s [-e] <PLAINTEXT\n", argv[0]);
fprintf(stderr, "\n");
/* Xor key with nonce */ fprintf(stderr, "You must pass in a key on fd 3 or in the environment variable KEY.\n");
for (i = 0; i < sizeof(key); i += 1) { return EX_USAGE;
key[i] ^= nonce[i];
}
arc4_init(&ctx, key, sizeof(key));
while (1) {
int c = getchar();
if (EOF == c) break;
putchar(c ^ arc4_out(&ctx));
} }
return 0; return 0;

View File

@ -12,6 +12,21 @@ struct arc4_ctx {
uint8_t j; uint8_t j;
}; };
/* Stream operations */
ssize_t
arc4_encrypt_stream(FILE *out, FILE *in,
const uint8_t *key, size_t keylen);
ssize_t
arc4_decrypt_stream(FILE *out, FILE *in,
const uint8_t *key, size_t keylen);
/* Auto-seeding Psuedo Random Number Generator */
void arc4_rand_seed(const uint8_t *seed, size_t seedlen);
uint8_t arc4_rand8();
uint32_t arc4_rand32();
/* Low-level operations */
void arc4_init(struct arc4_ctx *ctx, const uint8_t *key, size_t keylen); void arc4_init(struct arc4_ctx *ctx, const uint8_t *key, size_t keylen);
uint8_t arc4_out(struct arc4_ctx *ctx); uint8_t arc4_out(struct arc4_ctx *ctx);
void arc4_crypt(struct arc4_ctx *ctx, void arc4_crypt(struct arc4_ctx *ctx,
@ -19,4 +34,6 @@ void arc4_crypt(struct arc4_ctx *ctx,
void arc4_crypt_buffer(const uint8_t *key, size_t keylen, void arc4_crypt_buffer(const uint8_t *key, size_t keylen,
uint8_t *buf, size_t buflen); uint8_t *buf, size_t buflen);
void arc4_nonce(uint8_t *nonce, size_t noncelen, void *seed, size_t seedlen); void arc4_nonce(uint8_t *nonce, size_t noncelen, void *seed, size_t seedlen);
#endif #endif

View File

@ -1,65 +0,0 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include "arc4.h"
/*
*
* Random numbers
*
*/
void
urandom(uint8_t *buf, size_t buflen)
{
static int initialized = 0;
static struct arc4_ctx ctx;
if (! initialized) {
int fd = open("/dev/urandom", O_RDONLY);
if (-1 == fd) {
struct {
time_t time;
pid_t pid;
} bits;
bits.time = time(NULL);
bits.pid = getpid();
arc4_init(&ctx, (uint8_t *)&bits, sizeof(bits));
} else {
uint8_t key[256];
read(fd, key, sizeof(key));
close(fd);
arc4_init(&ctx, key, sizeof(key));
}
initialized = 1;
}
while (buflen--) {
*(buf++) = arc4_out(&ctx);
}
}
int32_t
rand32()
{
int32_t ret;
urandom((uint8_t *)&ret, sizeof(ret));
return ret;
}
uint32_t
randu32()
{
uint32_t ret;
urandom((uint8_t *)&ret, sizeof(ret));
return ret;
}

View File

@ -1,11 +0,0 @@
#ifndef __RAND_H__
#define __RAND_H__
#include <stdint.h>
#include <stddef.h>
void urandom(void *buf, size_t buflen);
int32_t rand32();
uint32_t randu32();
#endif /* __RAND_H__ */

106
src/tea.c
View File

@ -1,106 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include "xxtea.h"
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v)
#define min(a,b) (((a)<(b))?(a):(b))
int
main(int argc, char *argv[])
{
uint8_t *buf = NULL;
size_t len = 0;
uint32_t key[4] = {0};
int encode = 0;
/* Parse args */
{
int opt;
while ((opt = getopt(argc, argv, "e")) != -1) {
switch (opt) {
case 'e':
encode = 1;
break;
default:
fprintf(stderr, "Usage: %s [-e] <PLAINTEXT\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "You must pass in a key on fd 3 or in the environment variable KEY.\n");
return EX_USAGE;
}
}
}
/* Read key */
{
char *ekey = getenv("KEY");
if (ekey) {
size_t l = strlen(ekey);
memcpy(key, ekey, min(l, sizeof(key)));
} else {
read(3, key, sizeof(key));
}
}
while (1) {
size_t pos = len;
ssize_t nret;
buf = realloc(buf, len + 4096);
if (! buf) {
perror("realloc");
return EX_OSERR;
}
nret = read(0, buf + pos, 4096);
if (0 == nret) break;
if (-1 == nret) {
perror("read");
return EX_OSERR;
}
len = pos + nret;
}
if (argv[1] && (0 == strcmp(argv[1], "-e"))) {
if (0 == buf[len-1]) {
fprintf(stderr, "I can't cope with trailing NULs.\n");
return 1;
}
/* Pad out with NUL */
while (len % 4 > 0) {
buf[len++] = 0;
}
tea_encode(key, (uint32_t *)buf, len/4);
} else {
if (len % 4) {
fprintf(stderr, "Incorrect padding.\n");
return 1;
}
tea_decode(key, (uint32_t *)buf, len/4);
/* Remove padding. If your input had trailing NULs, you shouldn't
use this. */
while (0 == buf[len-1]) {
len -= 1;
}
}
write(1, buf, len);
return 0;
}

View File

@ -1,52 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "xxtea.h"
#define DELTA 0x9e3779b9
#define MX ((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z));
void
btea(uint32_t *v, int n, uint32_t const k[4])
{
uint32_t y, z, sum;
unsigned p, rounds, e;
if (n > 1) { /* Coding Part */
rounds = 6 + 52/n;
sum = 0;
z = v[n-1];
do {
sum += DELTA;
e = (sum >> 2) & 3;
for (p=0; p<n-1; p++)
y = v[p+1], z = v[p] += MX;
y = v[0];
z = v[n-1] += MX;
} while (--rounds);
} else if (n < -1) { /* Decoding Part */
n = -n;
rounds = 6 + 52/n;
sum = rounds*DELTA;
y = v[0];
do {
e = (sum >> 2) & 3;
for (p=n-1; p>0; p--)
z = v[p-1], y = v[p] -= MX;
z = v[n-1];
y = v[0] -= MX;
} while ((sum -= DELTA) != 0);
}
}
void
tea_encode(uint32_t const key[4], uint32_t *buf, size_t buflen)
{
btea(buf, buflen, key);
}
void
tea_decode(uint32_t const key[4], uint32_t *buf, size_t buflen)
{
btea(buf, -buflen, key);
}

View File

@ -1,9 +0,0 @@
#ifndef __XXTEA_H__
#define __XXTEA_H__
#include <stdint.h>
void tea_encode(uint32_t const key[4], uint32_t *buf, size_t buflen);
void tea_decode(uint32_t const key[4], uint32_t *buf, size_t buflen);
#endif