From f69073bfe9b8566e456c9e1771db9da9ddd7182d Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 7 Feb 2013 20:15:30 +0100 Subject: error in java version detected (c is identical) + c implemention other wise complete but not optimised (and misses blksize reading) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- c/sha3.c | 31 ++++++++++++++++++++++++++++--- c/sha3.h | 6 ++++++ c/sha3sum.c | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 63 insertions(+), 11 deletions(-) (limited to 'c') diff --git a/c/sha3.c b/c/sha3.c index 60d7089..de05328 100644 --- a/c/sha3.c +++ b/c/sha3.c @@ -34,8 +34,8 @@ #define min(X, Y) ((X) < (Y) ? (X) : (Y)) -#define arraycopy(src, soff, dest, doff, length) {long copyi; for (copyi = 0; copyi < length; copyi++) dest[copyi + soff] = src[copyi + doff];} -#define revarraycopy(src, soff, dest, doff, length) {long copyi; for (copyi = length - 1; copyi >= 0; copyi--) dest[copyi + soff] = src[copyi + doff];} +#define arraycopy(src, soff, dest, doff, length) {long copyi; for (copyi = 0; copyi < length; copyi++) dest[copyi + doff] = src[copyi + soff];} +#define revarraycopy(src, soff, dest, doff, length) {long copyi; for (copyi = length - 1; copyi >= 0; copyi--) dest[copyi + doff] = src[copyi + soff];} @@ -354,7 +354,7 @@ static byte* pad10star1(byte* msg, long len, long r, long* outlen) message = (byte*)malloc(len); message[nrf] = b; for (i = nrf + 1; i < len; i++) - message[i + nrf] = 0; + message[i] = 0; message[len - 1] = -128; } arraycopy(msg, 0, message, 0, nrf); @@ -373,6 +373,8 @@ static byte* pad10star1(byte* msg, long len, long r, long* outlen) */ extern void initialise(long bitrate, long capacity, long output) { + long i; + r = bitrate; n = output; c = capacity; @@ -384,8 +386,27 @@ extern void initialise(long bitrate, long capacity, long output) S = (llong*)malloc(25 * sizeof(llong)); M = (byte*)malloc(mlen = (r * b) >> 2); mptr = 0; + + for (i = 0; i < 25; i++) + *(S + i) = 0; } +/** + * Dispose of the Keccak sponge + */ +extern void dispose() +{ + if (S != null) + { + free(S); + S = null; + } + if (M != null) + { + free(M); + M = null; + } +} /** * Absorb the more of the message message to the Keccak sponge @@ -476,6 +497,8 @@ extern void update(byte* msg, long msglen) S[24] ^= toLane(message, len, rr, ww, i + 24 * w); keccakF(S); } + + free(message); } @@ -574,6 +597,8 @@ extern byte* digest(byte* msg, long msglen) keccakF(S); } + free(message); + /* Squeezing phase */ olen = n; ni = min(25, rr); diff --git a/c/sha3.h b/c/sha3.h index 8516690..faf94d1 100644 --- a/c/sha3.h +++ b/c/sha3.h @@ -30,6 +30,12 @@ extern void initialise(long bitrate, long capacity, long output); +/** + * Dispose of the Keccak sponge + */ +extern void dispose(); + + /** * Absorb the more of the message message to the Keccak sponge * diff --git a/c/sha3sum.c b/c/sha3sum.c index 4e2198b..993cf94 100644 --- a/c/sha3sum.c +++ b/c/sha3sum.c @@ -26,6 +26,8 @@ #define true 1 #define null 0 +#define HEXADECA "0123456789ABCDEF" + /** * String equality comparator @@ -53,8 +55,8 @@ long parseInt(char* str) { long rc = 0; while (*str) - rc = rc * 10 - (*str & 15); - return rc; + rc = rc * 10 - (*str++ & 15); + return -rc; } @@ -77,6 +79,8 @@ int main(int argc, char** argv) long a = 0, an = argc - 1; char** args = argv + 1; + *linger = 0; + s = -1; for (i = 0; *(cmd + i); i++) @@ -162,6 +166,10 @@ int main(int argc, char** argv) printf("\n"); fflush(stdout); fflush(stderr); + if (freelinger) + free(*linger); + free(linger); + free(files); return 2; } else @@ -188,12 +196,16 @@ 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 = true; + freelinger = false; *linger = null; if (arg == null) continue; @@ -245,7 +257,7 @@ int main(int argc, char** argv) { char* _ = (char*)malloc(3); *_++ = '-'; *_++ = *arg; *_ = 0; - linger[0] = _ - 3; + linger[0] = _ - 2; } { long _ = 0; @@ -269,6 +281,7 @@ int main(int argc, char** argv) fprintf(stderr, "%s: sorry, I will only do at least one iteration!\n", cmd); fflush(stdout); fflush(stderr); + free(files); return 3; } @@ -301,7 +314,7 @@ int main(int argc, char** argv) initialise(r, c, o); blksize = 4096; /** XXX os.stat(os.path.realpath(fn)).st_size; **/ - chunk = malloc(blksize); + chunk = (char*)malloc(blksize); for (;;) { long read = fread(chunk, 1, blksize, file); @@ -311,11 +324,15 @@ int main(int argc, char** argv) } free(chunk); bs = digest(null, 0); + dispose(); bn = (o + 7) >> 3; for (_ = 1; _ < i; _++) { + char* _ = bs; initialise(r, c, o); bs = digest(bs, bn); + free(_); + dispose(); } if (binary) @@ -340,8 +357,9 @@ int main(int argc, char** argv) rc = malloc((bn << 1) + 3 + (filename == null ? 1 : 0) + flen); for (b = 0; b < bn; b++) { - *(rc + rcptr++) = "0123456789ABCDEF"[(bs[b] >> 4) & 15]; - *(rc + rcptr++) = "0123456789ABCDEF"[bs[b] & 15]; + char v = bs[b]; + *(rc + rcptr++) = HEXADECA[(v >> 4) & 15]; + *(rc + rcptr++) = HEXADECA[v & 15]; } *(rc + rcptr++) = ' '; if (filename == null) @@ -373,7 +391,10 @@ int main(int argc, char** argv) fflush(stdout); fflush(stderr); if (fail) - return 5; + { + free(files); + return 5; + } } free(files); -- cgit v1.2.3-70-g09d2