aboutsummaryrefslogtreecommitdiffstats
path: root/c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--c/sha3.c75
-rw-r--r--c/sha3.h25
-rw-r--r--c/sha3sum.c106
3 files changed, 100 insertions, 106 deletions
diff --git a/c/sha3.c b/c/sha3.c
index 15cd07c..0edb46c 100644
--- a/c/sha3.c
+++ b/c/sha3.c
@@ -19,24 +19,30 @@
#include "sha3.h"
-#if __x86_64__ || __ppc64__
- #define llong long int
+#ifdef WITH_C99
+ #define static_inline static inline
#else
- #define llong long long int
+ #define static_inline inline
#endif
-
-#define null 0
-#define byte char
-#define boolean long
-#define true 1
-#define false 0
+#define null 0
+#define true 1
+#define false 0
/**
* Round contants
*/
+#ifdef WITH_C99
+static const llong RC[] = {
+ 0x0000000000000001LL, 0x0000000000008082LL, 0x800000000000808ALL, 0x8000000080008000LL,
+ 0x000000000000808BLL, 0x0000000080000001LL, 0x8000000080008081LL, 0x8000000000008009LL,
+ 0x000000000000008ALL, 0x0000000000000088LL, 0x0000000080008009LL, 0x000000008000000ALL,
+ 0x000000008000808BLL, 0x800000000000008BLL, 0x8000000000008089LL, 0x8000000000008003LL,
+ 0x8000000000008002LL, 0x8000000000000080LL, 0x000000000000800ALL, 0x800000008000000ALL,
+ 0x8000000080008081LL, 0x8000000000008080LL, 0x0000000080000001LL, 0x8000000080008008LL};
+#else
static const llong RC[] = {
0x0000000000000001L, 0x0000000000008082L, 0x800000000000808AL, 0x8000000080008000L,
0x000000000000808BL, 0x0000000080000001L, 0x8000000080008081L, 0x8000000000008009L,
@@ -44,6 +50,7 @@ static const llong RC[] = {
0x000000008000808BL, 0x800000000000008BL, 0x8000000000008089L, 0x8000000000008003L,
0x8000000000008002L, 0x8000000000000080L, 0x000000000000800AL, 0x800000008000000AL,
0x8000000080008081L, 0x8000000000008080L, 0x0000000080000001L, 0x8000000080008008L};
+#endif
/**
* Keccak-f round temporary
@@ -139,7 +146,7 @@ static long mlen = 0;
* @param doff The destination array offset
* @param length The number of elements to copy
*/
-inline void arraycopy(byte* src, long soff, byte* dest, long doff, long length)
+static_inline void arraycopy(byte* src, long soff, byte* dest, long doff, long length)
{
long i;
src += soff;
@@ -209,7 +216,7 @@ inline void arraycopy(byte* src, long soff, byte* dest, long doff, long length)
* @param doff The destination array offset
* @param length The number of elements to copy
*/
-inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long length)
+static_inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long length)
{
long copyi;
for (copyi = length - 1; copyi >= 0; copyi--)
@@ -234,7 +241,7 @@ inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long lengt
* @param N:long Rotation steps, may not be 0
* @return :llong The value rotated
*/
-#define rotate64(X, N) ((llong)((unsigned llong)(X) >> (64 - (N))) + ((X) << (N)))
+#define rotate64(X, N) ((llong)((ullong)(X) >> (64 - (N))) + ((X) << (N)))
/**
@@ -243,7 +250,7 @@ inline void revarraycopy(byte* src, long soff, byte* dest, long doff, long lengt
* @param x The value of which to calculate the binary logarithm
* @return The binary logarithm
*/
-static long lb(long x)
+static_inline long lb(long x)
{
long rc = 0;
if ((x & 0xFF00) != 0) { rc += 8; x >>= 8; }
@@ -260,7 +267,7 @@ static long lb(long x)
* @param A The current state
* @param rc Round constant
*/
-static void keccakFRound(llong* A, llong rc)
+static void keccakFRound(llong* restrict A, llong rc)
{
llong da, db, dc, dd, de;
@@ -321,7 +328,7 @@ static void keccakFRound(llong* A, llong rc)
*
* @param A The current state
*/
-static void keccakF(llong* A)
+static void keccakF(llong* restrict A)
{
long i;
if (nr == 24)
@@ -367,7 +374,7 @@ static void keccakF(llong* A)
* @param off The offset in the message
* @return Lane
*/
-inline llong toLane(byte* message, long msglen, long rr, long ww, long off)
+static_inline llong toLane(byte* restrict message, long msglen, long rr, long ww, long off)
{
llong rc = 0;
long n = min(msglen, rr), i;
@@ -386,7 +393,7 @@ inline llong toLane(byte* message, long msglen, long rr, long ww, long off)
* @param off The offset in the message
* @return Lane
*/
-inline llong toLane64(byte* message, long msglen, long rr, long off)
+static_inline llong toLane64(byte* restrict message, long msglen, long rr, long off)
{
long n = min(msglen, rr);
return ((off + 7 < n) ? ((llong)(message[off + 7] & 255) << 56) : 0L) |
@@ -409,7 +416,7 @@ inline llong toLane64(byte* message, long msglen, long rr, long off)
* @param outlen The length of the padded message (out parameter)
* @return The message padded
*/
-inline byte* pad10star1(byte* msg, long len, long r, long* outlen)
+static_inline byte* pad10star1(byte* restrict msg, long len, long r, long* restrict outlen)
{
byte* message;
@@ -422,7 +429,7 @@ inline byte* pad10star1(byte* msg, long len, long r, long* outlen)
if ((r - 8 <= ll) && (ll <= r - 2))
{
- message = (byte*)malloc(len = nrf + 1);
+ message = (byte*)malloc((len = nrf + 1) * sizeof(byte));
message[nrf] = (byte)(b ^ 128);
}
else
@@ -431,7 +438,7 @@ inline byte* pad10star1(byte* msg, long len, long r, long* outlen)
long N;
len = (nrf + 1) << 3;
len = ((len - (len % r) + (r - 8)) >> 3) + 1;
- message = (byte*)malloc(len);
+ message = (byte*)malloc(len * sizeof(byte));
message[nrf] = b;
N = len - nrf - 1;
M = message + nrf + 1;
@@ -525,7 +532,7 @@ extern void initialise(long bitrate, long capacity, long output)
wmod--;
}
S = (llong*)malloc(25 * sizeof(llong));
- M = (byte*)malloc(mlen = (r * b) >> 2);
+ M = (byte*)malloc((mlen = (r * b) >> 2) * sizeof(byte));
mptr = 0;
for (i = 0; i < 25; i++)
@@ -555,7 +562,7 @@ extern void dispose()
* @param msg The partial message
* @param msglen The length of the partial message
*/
-extern void update(byte* msg, long msglen)
+extern void update(byte* restrict msg, long msglen)
{
long rr = r >> 3;
long ww = w >> 3;
@@ -565,16 +572,11 @@ extern void update(byte* msg, long msglen)
long nnn;
if (mptr + msglen > mlen)
- {
- byte* buf = (byte*)malloc(mlen = (mlen + msglen) << 1);
- arraycopy(M, 0, buf, 0, mptr);
- free(M);
- M = buf;
- }
+ M = (byte*)realloc(M, mlen = (mlen + msglen) << 1);
arraycopy(msg, 0, M, mptr, msglen);
len = mptr += msglen;
len -= len % ((r * b) >> 3);
- message = (byte*)malloc(len);
+ message = (byte*)malloc(len * sizeof(byte));
arraycopy(M, 0, message, 0, len);
mptr -= len;
revarraycopy(M, nnn = len, M, 0, mptr);
@@ -622,7 +624,7 @@ extern void update(byte* msg, long msglen)
* @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null}
* @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false}
*/
-extern byte* digest(byte* msg, long msglen, boolean withReturn)
+extern byte* digest(byte* restrict msg, long msglen, boolean withReturn)
{
byte* message;
byte* _msg;
@@ -638,18 +640,13 @@ extern byte* digest(byte* msg, long msglen, boolean withReturn)
else
{
if (mptr + msglen > mlen)
- {
- byte* buf = (byte*)malloc(mlen += msglen);
- arraycopy(M, 0, buf, 0, mptr);
- free(M);
- M = buf;
- }
+ M = (byte*)realloc(M, mlen += msglen);
arraycopy(msg, 0, M, mptr, msglen);
message = pad10star1(M, mptr + msglen, r, &len);
}
free(M);
M = null;
- rc = (byte*)malloc((n + 7) >> 3);
+ rc = (byte*)malloc(((n + 7) >> 3) * sizeof(byte));
_msg = message;
nnn = len;
@@ -756,7 +753,7 @@ extern void fastSqueeze(long times)
*
* @return The hash sum
*/
-extern byte* squeeze()
+extern byte* squeeze(void)
{
long nn, ww, olen, i, j, ptr, ni;
byte* rc;
@@ -764,7 +761,7 @@ extern byte* squeeze()
keccakF(S); /* Last squeeze did not do a ending squeeze */
ww = w >> 3;
- rc = (byte*)malloc(nn = (n + 7) >> 3);
+ rc = (byte*)malloc((nn = (n + 7) >> 3) * sizeof(byte));
olen = n;
j = ptr = 0;
ni = (25 < r >> 3) ? 25 : (r >> 3);
diff --git a/c/sha3.h b/c/sha3.h
index 7371451..52fa8da 100644
--- a/c/sha3.h
+++ b/c/sha3.h
@@ -19,6 +19,25 @@
#include <stdlib.h>
+#ifdef WITH_C99
+ #include <inttypes.h>
+ #define byte int_fast8_t
+ #define boolean int_fast8_t
+ #define llong int_fast64_t
+ #define ullong uint_fast64_t
+#else
+ #define restrict /* introduced in C99 */
+ #define byte char
+ #define boolean char
+ #if __x86_64__ || __ppc64__
+ #define llong long int
+ #else
+ #define llong long long int
+ #endif
+ #define ullong unsigned llong
+#endif
+
+
/**
* Initialise Keccak sponge
@@ -42,7 +61,7 @@ extern void dispose(void);
* @param msg The partial message
* @param msglen The length of the partial message
*/
-extern void update(char* msg, long msglen);
+extern void update(byte* restrict msg, long msglen);
/**
@@ -53,7 +72,7 @@ extern void update(char* msg, long msglen);
* @param withReturn Whether to return the hash instead of just do a quick squeeze phrase and return {@code null}
* @return The hash sum, or {@code null} if <tt>withReturn</tt> is {@code false}
*/
-extern char* digest(char* msg, long msglen, long withReturn);
+extern byte* digest(byte* restrict msg, long msglen, boolean withReturn);
/**
@@ -77,5 +96,5 @@ extern void fastSqueeze(long times);
*
* @return The hash sum
*/
-extern char* squeeze(void);
+extern byte* squeeze(void);
diff --git a/c/sha3sum.c b/c/sha3sum.c
index fb62b93..87480ea 100644
--- a/c/sha3sum.c
+++ b/c/sha3sum.c
@@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
+#include <alloca.h>
#include <sys/stat.h>
#include "sha3.h"
@@ -34,13 +35,10 @@
/**
* Prints a number of bytes to stdout
*
- * @param bytes The bytes to print
- * @param n The number of bytes
+ * @param BYTES:char* The bytes to print
+ * @param N:long The number of bytes
*/
-inline void putchars(char* bytes, long n)
-{
- fwrite(bytes, 1, n, stdout);
-}
+#define putchars(BYTES, N) fwrite(BYTES, 1, N, stdout)
/**
@@ -63,7 +61,7 @@ SET set_new()
*
* @param set The set
*/
-void set_free(SET set)
+void set_free(SET restrict set)
{
if (*(set + 0)) set_free((void**)*(set + 0));
if (*(set + 1)) set_free((void**)*(set + 1));
@@ -92,7 +90,7 @@ void set_free(SET set)
* @param item The item
* @param n The length of the item
*/
-void set_add(SET set, char* item, long n)
+void set_add(SET restrict set, char* restrict item, long n)
{
long i, j;
void** at = set;
@@ -127,7 +125,7 @@ void set_add(SET set, char* item, long n)
* @param n The length of the item
* @return Whether the set contains the item
*/
-long set_contains(SET set, char* item, long n)
+long set_contains(SET restrict set, byte* restrict item, long n)
{
long i;
void** at = set;
@@ -154,7 +152,7 @@ long set_contains(SET set, char* item, long n)
* @param b Second comparand
* @return Whether the comparands are equal
*/
-long eq(char* a, char* b)
+long eq(char* restrict a, char* restrict b)
{
while (*a)
if (*a++ != *b++)
@@ -169,7 +167,7 @@ long eq(char* a, char* b)
* @param str String representation
* @return Native representation
*/
-long parseInt(char* str)
+long parseInt(char* restrict str)
{
long rc = 0;
while (*str)
@@ -187,18 +185,21 @@ long parseInt(char* str)
*/
int main(int argc, char** argv)
{
- char* cmd = *argv;
+ char* out_alloc;
+ byte* stdin_alloc;
long _o, o, _s, s, _r, r, _c, c, _w, w, _i, i, _j, j;
long _O, O, _S, S, _R, R, _C, C, _W, W, _I, I, _J, J;
- long binary = false, hex = false, dashed = false, freelinger = true;
+ long binary = false, hex = false, dashed = false;
long multi = 0, fptr = 0, bn;
- char** files = (char**)malloc(argc * sizeof(char*));
- char** linger = (char**)malloc(sizeof(char*) << 1);
+ char** files = (char**)alloca((argc + 2) * sizeof(char*));
+ char** linger = files + argc;
+ char* linger0 = (char*)alloca(sizeof(char) << 13);
long a = 0, an = argc - 1;
char** args = argv + 1;
+ char* cmd = *argv;
_O = _S = _R = _C = _W = _I = _J = false;
@@ -302,10 +303,6 @@ int main(int argc, char** argv)
printf("\n");
fflush(stdout);
fflush(stderr);
- if (freelinger)
- free(*linger);
- free(linger);
- free(files);
return 0;
}
else
@@ -334,16 +331,9 @@ int main(int argc, char** argv)
fprintf(stderr, "%s: unrecognised option: %s\n", cmd, *linger);
fflush(stdout);
fflush(stderr);
- if (freelinger)
- free(*linger);
- free(linger);
- free(files);
return 1;
}
}
- if (freelinger)
- free(*linger);
- freelinger = false;
*linger = null;
if (arg == null)
continue;
@@ -367,7 +357,7 @@ int main(int argc, char** argv)
}
if (idx >= 0)
{
- linger[0] = (char*)malloc(idx);
+ linger[0] = linger0;
linger[1] = arg + idx + 1;
for (j = 0; j < idx; j++)
*(*linger + j) = *(arg + j);
@@ -383,7 +373,6 @@ int main(int argc, char** argv)
{
linger[0] = arg;
linger[1] = null;
- freelinger = false;
}
}
else if ((arg[0] == '-') && arg[1])
@@ -407,7 +396,7 @@ int main(int argc, char** argv)
else
{
{
- char* _ = (char*)malloc(3);
+ char* _ = linger0;
*_++ = '-'; *_++ = *arg; *_ = 0;
linger[0] = _ - 2;
}
@@ -423,8 +412,6 @@ int main(int argc, char** argv)
files[fptr++] = arg;
}
- free(linger);
-
i = _I ? I : _i;
j = _J ? J : _j;
@@ -548,27 +535,27 @@ int main(int argc, char** argv)
if (i < 1)
{
ERR("sorry, I will only do at least one hash iteration!");
- free(files);
return 3;
}
if (j < 1)
{
ERR("sorry, I will only do at least one squeeze iteration!");
- free(files);
return 3;
}
#undef ERR
bn = (o + 7) >> 3;
+ out_alloc = (char*)alloca(bn * 2 * sizeof(char) + bn * sizeof(byte));
+ stdin_alloc = (byte*)(out_alloc + bn * 2);
{
- char* stdin;
+ byte* stdin;
char* filename;
char* fn;
long f, fail, _;
struct stat attr;
- char* out = binary ? null : (char*)malloc(bn * 2);
+ char* out = binary ? null : out_alloc;
fail = false;
stdin = null;
@@ -577,8 +564,8 @@ int main(int argc, char** argv)
{
FILE* file;
long blksize;
- char* chunk;
- char* bs;
+ byte* chunk;
+ byte* bs;
filename = *(files + f);
fn = filename ? filename : "/dev/stdin";
@@ -596,7 +583,7 @@ int main(int argc, char** argv)
blksize = stat(*(argv + f), &attr) ? 0 : attr.st_blksize;
if (blksize <= 0)
blksize = 4096;
- chunk = (char*)malloc(blksize);
+ chunk = (byte*)alloca(blksize * sizeof(byte));
for (;;)
{
long read = fread(chunk, 1, blksize, file);
@@ -609,7 +596,7 @@ int main(int argc, char** argv)
int n = read >> 1;
for (_ = 0; _ < n; _++)
{
- char a = *(chunk + (_ << 1)), b = *(chunk + ((_ << 1) | 1));
+ byte a = *(chunk + (_ << 1)), b = *(chunk + ((_ << 1) | 1));
a = (a & 15) + (a <= '9' ? 0 : 9);
b = (b & 15) + (b <= '9' ? 0 : 9);
*(chunk + _) = (a << 4) | b;
@@ -617,7 +604,6 @@ int main(int argc, char** argv)
update(chunk, n);
}
}
- free(chunk);
bs = digest(null, 0, j == 1);
if (j > 2)
fastSqueeze(j - 2);
@@ -627,9 +613,9 @@ int main(int argc, char** argv)
if (filename == null)
{
- stdin = (char*)malloc(bn * sizeof(char));
+ stdin = stdin_alloc;
for (_ = 0; _ < bn; _++)
- *(stdin + _) = *(bs + _);
+ *(stdin_alloc + _) = *(bs + _);
}
}
else
@@ -639,7 +625,7 @@ int main(int argc, char** argv)
{
for (_ = 1; _ < i; _++)
{
- char* _bs = bs;
+ byte* _bs = bs;
initialise(r, c, o);
bs = digest(bs, bn, j == 1);
if (j > 2)
@@ -650,13 +636,13 @@ int main(int argc, char** argv)
dispose();
}
if (binary)
- putchars(bs, bn);
+ putchars((char*)bs, bn);
else
{
long b, outptr = 0;
for (b = 0; b < bn; b++)
{
- char v = bs[b];
+ byte v = bs[b];
*(out + outptr++) = HEXADECA[(v >> 4) & 15];
*(out + outptr++) = HEXADECA[v & 15];
}
@@ -667,12 +653,12 @@ int main(int argc, char** argv)
{
long b;
if (binary)
- putchars(bs, bn);
+ putchars((char*)bs, bn);
else
{
for (b = 0; b < bn; b++)
{
- char v = bs[b];
+ byte v = bs[b];
out[b * 2 ] = HEXADECA[(v >> 4) & 15];
out[b * 2 + 1] = HEXADECA[v & 15];
}
@@ -680,7 +666,7 @@ int main(int argc, char** argv)
}
for (_ = 1; _ < i; _++)
{
- char* _bs = bs;
+ byte* _bs = bs;
initialise(r, c, o);
bs = digest(bs, bn, j == 1);
if (j > 2)
@@ -690,12 +676,12 @@ int main(int argc, char** argv)
free(_bs);
dispose();
if (binary)
- putchars(bs, bn);
+ putchars((char*)bs, bn);
else
{
for (b = 0; b < bn; b++)
{
- char v = bs[b];
+ byte v = bs[b];
out[b * 2 ] = HEXADECA[(v >> 4) & 15];
out[b * 2 + 1] = HEXADECA[v & 15];
}
@@ -705,14 +691,15 @@ int main(int argc, char** argv)
}
else
{
- long b, loophere;
+ long b;
+ char loophere;
char* loop = null;
SET got = set_new();
for (_ = 0; _ < i; _++)
{
if (_ > 0)
{
- char* _bs = bs;
+ byte* _bs = bs;
initialise(r, c, o);
bs = digest(bs, bn, j == 1);
if (j > 2)
@@ -724,7 +711,7 @@ int main(int argc, char** argv)
}
for (b = 0; b < bn; b++)
{
- char v = bs[b];
+ byte v = bs[b];
out[b * 2 ] = HEXADECA[(v >> 4) & 15];
out[b * 2 + 1] = HEXADECA[v & 15];
}
@@ -732,7 +719,7 @@ int main(int argc, char** argv)
{
if (set_contains(got, bs, bn))
{
- loop = (char*)malloc(bn * 2);
+ loop = (char*)malloc(bn * 2 * sizeof(char));
for (b = 0; b < bn * 2; b++)
*(loop + b) = *(out + b);
}
@@ -759,21 +746,12 @@ int main(int argc, char** argv)
fclose(file);
}
- if (out != null)
- free(out);
- if (stdin != null)
- free(stdin);
-
fflush(stdout);
fflush(stderr);
if (fail)
- {
- free(files);
- return 5;
- }
+ return 5;
}
- free(files);
return 0;
}