aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-02-05 16:48:09 +0100
committerMattias Andrée <maandree@operamail.com>2013-02-05 16:48:09 +0100
commit0e4ee7b1718d23c3ebc4e2dd35af2f0d4b2d05ca (patch)
tree7b8ffdda4be4f9dd429f91f7ec5774a23fa37cf4
parentupdate status (diff)
downloadsha3sum-0e4ee7b1718d23c3ebc4e2dd35af2f0d4b2d05ca.tar.gz
sha3sum-0e4ee7b1718d23c3ebc4e2dd35af2f0d4b2d05ca.tar.bz2
sha3sum-0e4ee7b1718d23c3ebc4e2dd35af2f0d4b2d05ca.tar.xz
beginning of porting the command to c
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--.gitignore3
-rw-r--r--Makefile2
-rw-r--r--c/sha3.c2
-rw-r--r--c/sha3.h49
-rw-r--r--c/sha3sum.c304
-rw-r--r--pure-java/sha3sum.java8
6 files changed, 363 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 637b19a..3fdc9f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,3 +9,6 @@ __pycache__/
*.class
*.jar
*.pyc
+*.o
+*.out
+*.so
diff --git a/Makefile b/Makefile
index d0a0cdb..50208d9 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,6 @@ bin/pure-java/%.class: pure-java/%.java
.PHONY: clean
clean:
rm {*/,}*.{t2d,aux,cp,cps,fn,ky,log,pg,pgs,toc,tp,vr,vrs,op,ops} 2>/dev/null || exit 0
- rm {*/,}*.{bak,info,pdf,ps,dvi,gz,class,jar,pyc,o,so} 2>/dev/null || exit 0
+ rm {*/,}*.{bak,info,pdf,ps,dvi,gz,class,jar,pyc,o,so,out} 2>/dev/null || exit 0
rm -r bin 2>/dev/null || exit 0
diff --git a/c/sha3.c b/c/sha3.c
index be2adf5..60d7089 100644
--- a/c/sha3.c
+++ b/c/sha3.c
@@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h>
+#include "sha3.h"
#if __x86_64__ || __ppc64__
diff --git a/c/sha3.h b/c/sha3.h
new file mode 100644
index 0000000..8516690
--- /dev/null
+++ b/c/sha3.h
@@ -0,0 +1,49 @@
+/**
+ * sha3sum – SHA-3 (Keccak) checksum calculator
+ *
+ * Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdlib.h>
+
+
+
+/**
+ * Initialise Keccak sponge
+ *
+ * @param bitrate The bitrate
+ * @param capacity The capacity
+ * @param output The output size
+ */
+extern void initialise(long bitrate, long capacity, long output);
+
+
+/**
+ * Absorb the more of the message message to the Keccak sponge
+ *
+ * @param msg The partial message
+ * @param msglen The length of the partial message
+ */
+extern void update(char* msg, long msglen);
+
+
+/**
+ * Absorb the last part of the message and squeeze the Keccak sponge
+ *
+ * @param msg The rest of the message, may be {@code null}
+ * @param msglen The length of the partial message
+ */
+extern char* digest(char* msg, long msglen);
+
diff --git a/c/sha3sum.c b/c/sha3sum.c
new file mode 100644
index 0000000..026ab6d
--- /dev/null
+++ b/c/sha3sum.c
@@ -0,0 +1,304 @@
+/**
+ * sha3sum – SHA-3 (Keccak) checksum calculator
+ *
+ * Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sha3.h"
+
+
+#define false 0
+#define true 1
+#define null 0
+
+
+/**
+ * String equality comparator
+ *
+ * @param a First comparand
+ * @param b Second comparand
+ * @return Whether the comparands are equal
+ */
+long eq(char* a, char* b)
+{
+ while (*a)
+ if (*a++ != *b++)
+ return false;
+ return true;
+}
+
+
+/**
+ * Convert a string to an integer
+ *
+ * @param str String representation
+ * @return Native representation
+ */
+long parseInt(char* str)
+{
+ long rc = 0;
+ while (*str)
+ rc = rc * 10 - (*str & 15);
+ return rc;
+}
+
+
+/**
+ * This is the main entry point of the program
+ *
+ * @param argc Command line argument count
+ * @param argv Command line arguments
+ * @return Exit value, zero on and only on successful execution
+ */
+int main(int argc, char** argv)
+{
+ char* cmd = *argv;
+ long _o, o, _s, s, _r, r, _c, c, _w, w, _i, i;
+ long binary = false, dashed = false, fptr = 0, freelinger = true;
+
+ char** files = (char**)malloc(argc * sizeof(char*));
+ char** linger = (char**)malloc(sizeof(char*) << 1);
+
+ long a = 0, an = argc - 1;
+ char** args = argv + 1;
+
+
+ s = -1;
+ for (i = 0; *(cmd + i); i++)
+ if (*(cmd + i) == '/')
+ s = i;
+ if (s >= 0)
+ cmd += s + 1;
+
+ o = _o = 512; /* --outputsize */
+ if ((cmd[0] == 's') && (cmd[1] == 'h') && (cmd[2] == 'a') && (cmd[3] == '3') && (cmd[4] == '-'))
+ if ((cmd[5] != 0) && (cmd[6] != 0) && (cmd[7] != 0))
+ if ((cmd[8] == 's') && (cmd[9] == 'u') && (cmd[10] == 'm') && (cmd[11] == 0))
+ {
+ if ((cmd[5] == '2') && (cmd[6] == '2') && (cmd[7] == '4'))
+ o = _o = 224;
+ else if ((cmd[5] == '2') && (cmd[6] == '5') && (cmd[7] == '6'))
+ o = _o = 256;
+ else if ((cmd[5] == '3') && (cmd[6] == '8') && (cmd[7] == '4'))
+ o = _o = 384;
+ else if ((cmd[5] == '5') && (cmd[6] == '1') && (cmd[7] == '2'))
+ o = _o = 512;
+ }
+ s = _s = 1600; /* --statesize */
+ r = _r = s - (o << 1); /* --bitrate */
+ c = _c = s - r; /* --capacity */
+ w = _w = s / 25; /* --wordsize */
+ i = _i = 1; /* --iterations */
+
+
+ for (; a <= an; a++)
+ {
+ char* arg = a == an ? null : *(args + a);
+ if (*linger)
+ {
+ if (eq(*linger, "-h") || eq(*linger, "--help"))
+ {
+ printf("\n");
+ printf("SHA-3/Keccak checksum calculator\n");
+ printf("\n");
+ printf("USAGE: sha3sum [option...] < file\n");
+ printf(" sha3sum [option...] file...\n");
+ printf("\n");
+ printf("\n");
+ printf("OPTIONS:\n");
+ printf(" -r BITRATE\n");
+ printf(" --bitrate The bitrate to use for SHA-3. (default: %li)\n", _r);
+ printf(" \n");
+ printf(" -c CAPACITY\n");
+ printf(" --capacity The capacity to use for SHA-3. (default: %li)\n", _c);
+ printf(" \n");
+ printf(" -w WORDSIZE\n");
+ printf(" --wordsize The word size to use for SHA-3. (default: %li)\n", _w);
+ printf(" \n");
+ printf(" -o OUTPUTSIZE\n");
+ printf(" --outputsize The output size to use for SHA-3. (default: %li)\n", _o);
+ printf(" \n");
+ printf(" -s STATESIZE\n");
+ printf(" --statesize The state size to use for SHA-3. (default: %li)\n", _s);
+ printf(" \n");
+ printf(" -i ITERATIONS\n");
+ printf(" --iterations The number of hash iterations to run. (default: %li)\n", _i);
+ printf(" \n");
+ printf(" -b\n");
+ printf(" --binary Print the checksum in binary, rather than hexadecimal.\n");
+ printf("\n");
+ printf("\n");
+ printf("COPYRIGHT:\n");
+ printf("\n");
+ printf("Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)\n");
+ printf("\n");
+ printf("This program is free software: you can redistribute it and/or modify\n");
+ printf("it under the terms of the GNU General Public License as published by\n");
+ printf("the Free Software Foundation, either version 3 of the License, or\n");
+ printf("(at your option) any later version.\n");
+ printf("\n");
+ printf("This program is distributed in the hope that it will be useful,\n");
+ printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
+ printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n");
+ printf("GNU General Public License for more details.\n");
+ printf("\n");
+ printf("You should have received a copy of the GNU General Public License\n");
+ printf("along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
+ printf("\n");
+ fflush(stdout);
+ fflush(stderr);
+ return 2;
+ }
+ else
+ {
+ if (*(linger + 1) == null)
+ {
+ *(linger + 1) = arg;
+ arg = null;
+ }
+ if (eq(*linger, "-r") || eq(*linger, "--bitrate"))
+ o = (s - (r = parseInt(linger[1]))) >> 1;
+ else if (eq(*linger, "-c") || eq(*linger, "--capacity"))
+ r = s - (c = parseInt(linger[1]));
+ else if (eq(*linger, "-w") || eq(*linger, "--wordsize"))
+ s = (w = parseInt(linger[1])) * 25;
+ else if (eq(*linger, "-o") || eq(*linger, "--outputsize"))
+ r = s - ((o = parseInt(linger[1])) << 1);
+ else if (eq(*linger, "-s") || eq(*linger, "--statesize"))
+ r = (s = parseInt(linger[1])) - (o << 1);
+ else if (eq(*linger, "-i") || eq(*linger, "--iterations"))
+ i = parseInt(linger[1]);
+ else
+ {
+ fprintf(stderr, "%s: unrecognised option: %s\n", cmd, *linger);
+ fflush(stdout);
+ fflush(stderr);
+ return 1;
+ }
+ }
+ if (freelinger)
+ free(*linger);
+ freelinger = true;
+ *linger = null;
+ if (arg == null)
+ continue;
+ }
+ if (arg == null)
+ continue;
+ if (dashed)
+ files[fptr++] = ((arg[0] == '-') && (arg[1] == 0)) ? null : arg;
+ else if ((arg[0] == '-') && (arg[1] == '-') && (arg[2] == 0))
+ dashed = true;
+ else if ((arg[0] == '-') && (arg[1] == 0))
+ files[fptr++] = null;
+ else if ((arg[0] == '-') && (arg[1] == '-') && arg[2])
+ {
+ long idx = -1, j;
+ for (j = 0; *(arg + j); j++)
+ if (*(arg + j) == '=')
+ {
+ idx = j;
+ break;
+ }
+ if (idx >= 0)
+ {
+ linger[0] = (char*)malloc(idx);
+ linger[1] = arg + idx + 1;
+ for (j = 0; j < idx; j++)
+ *(*linger + j) = *(arg + j);
+ }
+ else
+ if (eq(arg, "--binary"))
+ binary = true;
+ else
+ {
+ linger[0] = arg;
+ linger[1] = null;
+ freelinger = false;
+ }
+ }
+ else if ((arg[0] == '-') && arg[1])
+ {
+ arg++;
+ if (*arg == 'b')
+ {
+ binary = true;
+ arg++;
+ }
+ else
+ {
+ {
+ char* _ = (char*)malloc(3);
+ *_++ = '-'; *_++ = *arg; *_ = 0;
+ linger[0] = _ - 3;
+ }
+ {
+ long _ = 0;
+ while (*(arg + _))
+ _++;
+ linger[1] = _ == 1 ? null : arg + 1;
+ }
+ }
+ }
+ else
+ files[fptr++] = arg;
+ }
+
+ free(linger);
+
+
+ if (fptr == 0)
+ files[fptr++] = null;
+ if (i < 1)
+ {
+ fprintf(stderr, "%s: sorry, I will only do at least one iteration!\n", cmd);
+ fflush(stdout);
+ fflush(stderr);
+ return 3;
+ }
+
+ {
+ char* stdin = null;
+ char* filename;
+ char* fn;
+ long f, fail = false;
+
+ for (f = 0; f < fptr; f++)
+ {
+ if (((filename = *(files + f)) == null) && stdin)
+ {
+ printf("%s", stdin);
+ continue;
+ }
+ fn = filename ? filename : "/dev/stdin";
+ /* String rc = ""; */
+ /* InputStream file = null; */
+
+ /* --------------------------------------------------------------- */
+ }
+
+ fflush(stdout);
+ fflush(stderr);
+ if (fail)
+ return 5;
+ }
+
+ free(files);
+ return 0;
+}
+
diff --git a/pure-java/sha3sum.java b/pure-java/sha3sum.java
index ec00958..79a436f 100644
--- a/pure-java/sha3sum.java
+++ b/pure-java/sha3sum.java
@@ -52,7 +52,7 @@ public class sha3sum
if (cmd.indexOf('/') >= 0)
cmd = cmd.substring(cmd.lastIndexOf('/') + 1);
if (cmd.endsWith(".jar"))
- cmd = cmd.substring(0, cmd.length() - 3);
+ cmd = cmd.substring(0, cmd.length() - 4);
cmd = cmd.intern();
int _o, o = _o = 512; /* --outputsize */
@@ -60,7 +60,7 @@ public class sha3sum
else if (cmd == "sha3-256sum") o = _o = 256;
else if (cmd == "sha3-384sum") o = _o = 384;
else if (cmd == "sha3-512sum") o = _o = 512;
- int _s, s = _s = 1600; /* --statesiz e */
+ int _s, s = _s = 1600; /* --statesize */
int _r, r = _r = s - (o << 1); /* --bitrate */
int _c, c = _c = s - r; /* --capacity */
int _w, w = _w = s / 25; /* --wordsize */
@@ -82,6 +82,7 @@ public class sha3sum
linger[0] = linger[0].intern();
if ((linger[0] == "-h") || (linger[0] == "--help"))
{
+ System.out.println("");
System.out.println("SHA-3/Keccak checksum calculator");
System.out.println("");
System.out.println("USAGE: sha3sum [option...] < file");
@@ -127,6 +128,7 @@ public class sha3sum
System.out.println("");
System.out.println("You should have received a copy of the GNU General Public License");
System.out.println("along with this program. If not, see <http://www.gnu.org/licenses/>.");
+ System.out.println("");
System.exit(2);
}
else
@@ -195,7 +197,7 @@ public class sha3sum
files[fptr++] = null;
if (i < 1)
{
- System.err.println(cmd + ": sorry, I will only do at least one iteration!\n");
+ System.err.println(cmd + ": sorry, I will only do at least one iteration!");
System.exit(3);
}