aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSathariel <sathariel@cock.li>2018-06-19 12:00:24 -0500
committerSathariel <sathariel@cock.li>2018-06-19 12:00:24 -0500
commit94d8a69b6828b086a48d9014ff640c2b951b28e8 (patch)
treefa8dbced482f285e6c1346ec6d77145777b23f55
downloadathame-94d8a69b6828b086a48d9014ff640c2b951b28e8.tar
athame-94d8a69b6828b086a48d9014ff640c2b951b28e8.zip
Initial commit
-rw-r--r--LICENSE5
-rw-r--r--Makefile23
-rw-r--r--README12
-rw-r--r--arg.h65
-rw-r--r--main.c134
5 files changed, 239 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..65d17f0
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,5 @@
+Copyright 2018 Sathariel
+
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..df84ee0
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,23 @@
+NAME = athame
+
+CC = cc
+CPPFLAGS =
+CFLAGS = -std=c99 -pedantic -Wall -Os
+LDFLAGS =
+PREFIX = /usr
+
+all: ${NAME}
+
+${NAME}: main.c
+ ${CC} ${CPPFLAGS} ${CFLAGS} -o $@ main.c ${LDFLAGS}
+
+clean:
+ rm -f ${NAME}
+
+install: all
+ mkdir -p ${DESTDIR}${PREFIX}/bin
+ cp -f ${NAME} ${DESTDIR}${PREFIX}/bin
+ chmod 755 ${DESTDIR}${PREFIX}/bin/${NAME}
+
+uninstall:
+ rm -f ${DESTDIR}${PREFIX}/bin/${NAME}
diff --git a/README b/README
new file mode 100644
index 0000000..b14a703
--- /dev/null
+++ b/README
@@ -0,0 +1,12 @@
+Implementation of CipherSaber ( http://ciphersaber.gurus.org/ )
+
+Usage:
+
+athame [-e | -d] [-k key] [-o file] file
+
+Options:
+
+-e Encrypt mode (default)
+-d Decrypt mode
+-k Key used to encrypt/decrypt. It will be prompted if this is ommited
+-o Output file. It uses standard output by default
diff --git a/arg.h b/arg.h
new file mode 100644
index 0000000..0b23c53
--- /dev/null
+++ b/arg.h
@@ -0,0 +1,65 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
+ argv[0] && argv[0][0] == '-'\
+ && argv[0][1];\
+ argc--, argv++) {\
+ char argc_;\
+ char **argv_;\
+ int brk_;\
+ if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ for (brk_ = 0, argv[0]++, argv_ = argv;\
+ argv[0][0] && !brk_;\
+ argv[0]++) {\
+ if (argv_ != argv)\
+ break;\
+ argc_ = argv[0][0];\
+ switch (argc_)
+
+/* Handles obsolete -NUM syntax */
+#define ARGNUM case '0':\
+ case '1':\
+ case '2':\
+ case '3':\
+ case '4':\
+ case '5':\
+ case '6':\
+ case '7':\
+ case '8':\
+ case '9'
+
+#define ARGEND }\
+ }
+
+#define ARGC() argc_
+
+#define ARGNUMF() (brk_ = 1, estrtonum(argv[0], 0, INT_MAX))
+
+#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
+ ((x), abort(), (char *)0) :\
+ (brk_ = 1, (argv[0][1] != '\0')?\
+ (&argv[0][1]) :\
+ (argc--, argv++, argv[0])))
+
+#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
+ (char *)0 :\
+ (brk_ = 1, (argv[0][1] != '\0')?\
+ (&argv[0][1]) :\
+ (argc--, argv++, argv[0])))
+
+#define LNGARG() &argv[0][0]
+
+#endif
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..dd6de1e
--- /dev/null
+++ b/main.c
@@ -0,0 +1,134 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "arg.h"
+
+#define KEYSIZE 64
+
+static unsigned char S[256];
+
+void
+swap(int i, int j)
+{
+ int t = S[i];
+ S[i] = S[j];
+ S[j] = t;
+}
+
+void
+ksa(char *key, size_t len)
+{
+ int i, j = 0;
+ for (i = 0; i < 256; i++)
+ S[i] = i;
+ for (i = 0; i < 256; i++) {
+ j = (j + S[i] + key[i % len]) & 255;
+ swap(i, j);
+ }
+}
+
+unsigned char
+rand4(void)
+{
+ static int i = 0, j = 0;
+ i = (i + 1) & 255;
+ j = (j + S[i]) & 255;
+ swap(i, j);
+ return S[(S[i] + S[j]) & 255];
+}
+
+unsigned char *
+genIV(FILE *fp)
+{
+ int i;
+ unsigned char *iv;
+ iv = malloc(10);
+ srand(time(NULL));
+ for (i = 0; i < 10; i++)
+ iv[i] = rand() & 255;
+ fwrite(iv, 1, 10, fp);
+ return iv;
+}
+
+unsigned char *
+getIV(FILE *fp)
+{
+ unsigned char *iv;
+ iv = malloc(10);
+ fread(iv, 1, 10, fp);
+ return iv;
+}
+
+void
+usage(char *name)
+{
+ fprintf(stderr, "usage: %s [-e | -d] [-k key] [-o file] file\n",
+ name);
+ exit(1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ int c;
+ char key[KEYSIZE + 10] = "", mode = 'e', *out;
+ unsigned char *iv;
+ size_t len;
+ FILE *infile, *outfile;
+ char *argv0;
+
+ outfile = stdout;
+ out = NULL;
+
+ ARGBEGIN {
+ case 'd':
+ case 'e':
+ mode = ARGC();
+ break;
+ case 'k':
+ strncpy(key, EARGF(usage(argv0)), KEYSIZE);
+ break;
+ case 'o':
+ out = EARGF(usage(argv0));
+ break;
+ default:
+ usage(argv0);
+ } ARGEND
+
+ if (argc < 1) {
+ usage(argv0);
+ return 1;
+ }
+
+ if (out && !(outfile = fopen(out, "w"))) {
+ perror("Error");
+ return 1;
+ }
+
+ if (!(infile = fopen(argv[0], "r"))) {
+ perror("Error");
+ return 1;
+ }
+
+ if (!*key) {
+ fprintf(stderr, "Enter key: ");
+ if (!fgets(key, KEYSIZE, stdin)) {
+ return 1;
+ } else {
+ key[strlen(key) - 1] = '\0';
+ }
+ }
+
+ iv = (mode == 'e') ? genIV(outfile) : getIV(infile);
+ len = strlen(key);
+ memmove(key + len, iv, 10);
+ ksa(key, len + 10);
+
+ while ((c = getc(infile)) != EOF)
+ putc(c ^ rand4(), outfile);
+ fclose(infile);
+ fclose(outfile);
+ return 0;
+} \ No newline at end of file