diff options
author | Mattias Andrée <maandree@kth.se> | 2019-02-11 17:56:37 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2019-02-11 17:56:37 +0100 |
commit | 5cee9b9c6394cffee6f31fab00323d9e559f0702 (patch) | |
tree | d21ba3da234c46b4f3c96e0065eb83d8c3cc8e96 | |
parent | Deprecate typedefs (diff) | |
download | libkeccak-5cee9b9c6394cffee6f31fab00323d9e559f0702.tar.gz libkeccak-5cee9b9c6394cffee6f31fab00323d9e559f0702.tar.bz2 libkeccak-5cee9b9c6394cffee6f31fab00323d9e559f0702.tar.xz |
General improvements
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | benchmark.c | 57 | ||||
-rw-r--r-- | common.h | 2 | ||||
-rw-r--r-- | digest.c | 47 | ||||
-rw-r--r-- | libkeccak.h | 14 | ||||
-rw-r--r-- | libkeccak_behex_lower.c | 1 | ||||
-rw-r--r-- | libkeccak_behex_upper.c | 1 | ||||
-rw-r--r-- | libkeccak_degeneralise_spec.c | 54 | ||||
-rw-r--r-- | libkeccak_generalised_sum_fd.c | 10 | ||||
-rw-r--r-- | libkeccak_hmac_copy.c | 2 | ||||
-rw-r--r-- | libkeccak_hmac_digest.c | 13 | ||||
-rw-r--r-- | libkeccak_hmac_fast_digest.c | 14 | ||||
-rw-r--r-- | libkeccak_hmac_fast_update.c | 10 | ||||
-rw-r--r-- | libkeccak_hmac_set_key.c | 4 | ||||
-rw-r--r-- | libkeccak_hmac_unmarshal.c | 2 | ||||
-rw-r--r-- | libkeccak_hmac_update.c | 9 | ||||
-rw-r--r-- | libkeccak_hmac_wipe.c | 4 | ||||
-rw-r--r-- | libkeccak_state_initialise.c | 4 | ||||
-rw-r--r-- | libkeccak_state_marshal.c | 2 | ||||
-rw-r--r-- | libkeccak_state_unmarshal.c | 2 | ||||
-rw-r--r-- | libkeccak_state_unmarshal_skip.c | 2 | ||||
-rw-r--r-- | libkeccak_state_wipe_message.c | 3 | ||||
-rw-r--r-- | libkeccak_state_wipe_sponge.c | 1 | ||||
-rw-r--r-- | libkeccak_unhex.c | 3 | ||||
-rw-r--r-- | test.c | 316 |
24 files changed, 352 insertions, 225 deletions
diff --git a/benchmark.c b/benchmark.c index 3554165..488d892 100644 --- a/benchmark.c +++ b/benchmark.c @@ -60,42 +60,59 @@ main(void) char hexsum[OUTPUT / 8 * 2 + 1]; #endif struct timespec start, end; - long i, r; - - /* Fill message with content from the file. */ + long int i, r; int fd; ssize_t got; size_t ptr; - if (fd = open(MESSAGE_FILE, O_RDONLY), fd < 0) - return perror("open"), 1; - for (ptr = 0; ptr < MESSAGE_LEN; ptr += (size_t)got) - if (got = read(fd, message, MESSAGE_LEN - ptr), got <= 0) - return perror("read"), close(fd), 1; + + /* Fill message with content from the file. */ + fd = open(MESSAGE_FILE, O_RDONLY); + if (fd < 0) { + perror("open"); + return 1; + } + for (ptr = 0; ptr < MESSAGE_LEN; ptr += (size_t)got) { + got = read(fd, message, MESSAGE_LEN - ptr); + if (got <= 0) { + perror("read"); + close(fd); + return 1; + } + } close(fd); /* Initialise state. */ spec.bitrate = BITRATE; spec.capacity = CAPACITY; spec.output = OUTPUT; - if (libkeccak_state_initialise(&state, &spec)) - return perror("libkeccak_state_initialise"), 1; + if (libkeccak_state_initialise(&state, &spec)) { + perror("libkeccak_state_initialise"); + return 1; + } /* Get start-time. */ - if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) < 0) - return perror("clock_gettime"), 1; + if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start) < 0) { + perror("clock_gettime"); + return 1; + } /* Run benchmarking loop. */ for (r = 0; r < RERUNS; r++) { /* Updates. */ #if UPDATE_RUNS > 0 - for (i = 0; i < UPDATE_RUNS; i++) - if (libkeccak_fast_update(&state, message, MESSAGE_LEN) < 0) - return perror("libkeccak_update"), 1; + for (i = 0; i < UPDATE_RUNS; i++) { + if (libkeccak_fast_update(&state, message, MESSAGE_LEN) < 0) { + perror("libkeccak_update"); + return 1; + } + } #endif /* Digest. */ - if (libkeccak_fast_digest(&state, NULL, 0, 0, NULL, hashsum) < 0) - return perror("libkeccak_digest"), 1; + if (libkeccak_fast_digest(&state, NULL, 0, 0, NULL, hashsum) < 0) { + perror("libkeccak_digest"); + return 1; + } #ifndef IGNORE_BEHEXING libkeccak_behex_lower(hexsum, hashsum, OUTPUT / 8); #endif @@ -117,8 +134,10 @@ main(void) } /* Get end-time. */ - if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0) - return perror("clock_gettime"), -1; + if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0) { + perror("clock_gettime"); + return -1; + } /* Print execution-time. */ end.tv_sec -= start.tv_sec; @@ -28,9 +28,11 @@ #define HMAC_INNER_PAD 0x36 +#ifdef NEED_EXPLICIT_BZERO static void *(*volatile my_explicit_memset)(void *, int, size_t) = memset; static __attribute__((__optimize__("-O0"))) void my_explicit_bzero(void *ptr, size_t size) { (*my_explicit_memset)(ptr, 0, size); } +#endif @@ -13,15 +13,10 @@ #define LIST_8 LIST_5 X(5) X(6) X(7) /** - * X-macro-enabled listing of all intergers in [0, 23] - */ -#define LIST_24 LIST_8 X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15)\ - X(16) X(17) X(18) X(19) X(20) X(21) X(22) X(23) - -/** * X-macro-enabled listing of all intergers in [0, 24] */ -#define LIST_25 LIST_24 X(24) +#define LIST_25 LIST_8 X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15)\ + X(16) X(17) X(18) X(19) X(20) X(21) X(22) X(23) X(24) @@ -198,7 +193,7 @@ libkeccak_f(register struct libkeccak_state *restrict state) */ LIBKECCAK_GCC_ONLY(__attribute__((__nonnull__, __nothrow__, __pure__, __warn_unused_result__, __gnu_inline__))) static inline int_fast64_t -libkeccak_to_lane(register const char *restrict message, register size_t msglen, +libkeccak_to_lane(register const unsigned char *restrict message, register size_t msglen, register long int rr, register long int ww, size_t off) { register long int n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off); @@ -223,7 +218,7 @@ libkeccak_to_lane(register const char *restrict message, register size_t msglen, */ LIBKECCAK_GCC_ONLY(__attribute__((__nonnull__, __nothrow__, __pure__, __hot__, __warn_unused_result__, __gnu_inline__))) static inline int_fast64_t -libkeccak_to_lane64(register const char *restrict message, register size_t msglen, register long int rr, size_t off) +libkeccak_to_lane64(register const unsigned char *restrict message, register size_t msglen, register long int rr, size_t off) { register long int n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off); int_fast64_t rc = 0; @@ -251,10 +246,10 @@ libkeccak_pad10star1(register struct libkeccak_state *restrict state, register s register size_t nrf = state->mptr - !!bits; register size_t len = (nrf << 3) | bits; register size_t ll = len % r; - register char b = (char)(bits ? (state->M[nrf] | (1 << bits)) : 1); + register unsigned char b = (unsigned char)(bits ? (state->M[nrf] | (1 << bits)) : 1); if (r - 8 <= ll && ll <= r - 2) { - state->M[nrf] = (char)(b ^ 0x80); + state->M[nrf] = (unsigned char)(b ^ 0x80); state->mptr = nrf + 1; } else { len = ++nrf << 3; @@ -263,7 +258,7 @@ libkeccak_pad10star1(register struct libkeccak_state *restrict state, register s state->M[nrf - 1] = b; __builtin_memset(state->M + nrf, 0, (len - nrf) * sizeof(char)); - state->M[len] = (char)0x80; + state->M[len] = (unsigned char)0x80; } } @@ -281,7 +276,7 @@ libkeccak_absorption_phase(register struct libkeccak_state *restrict state, regi register long int rr = state->r >> 3; register long int ww = state->w >> 3; register long int n = (long)len / rr; - register const char* restrict message = state->M; + register const unsigned char *restrict message = state->M; if (__builtin_expect(ww >= 8, 1)) { /* ww > 8 is impossible, it is just for optimisation possibilities. */ while (n--) { #define X(N) state->S[N] ^= libkeccak_to_lane64(message, len, rr, (size_t)(LANE_TRANSPOSE_MAP[N] * 8)); @@ -350,7 +345,7 @@ int libkeccak_fast_update(struct libkeccak_state *restrict state, const void *restrict msg, size_t msglen) { size_t len; - auto char *restrict new; + auto unsigned char *restrict new; if (__builtin_expect(state->mptr + msglen > state->mlen, 0)) { state->mlen += msglen; @@ -386,12 +381,12 @@ int libkeccak_update(struct libkeccak_state *restrict state, const void *restrict msg, size_t msglen) { size_t len; - auto char *restrict new; + auto unsigned char *restrict new; if (__builtin_expect(state->mptr + msglen > state->mlen, 0)) { state->mlen += msglen; new = malloc(state->mlen * sizeof(char)); - if (new == NULL) + if (!new) return state->mlen -= msglen, -1; libkeccak_state_wipe_message(state); free(state->M); @@ -427,8 +422,8 @@ int libkeccak_fast_digest(struct libkeccak_state *restrict state, const void *restrict msg_, size_t msglen, size_t bits, const char *restrict suffix, void *restrict hashsum) { - const char *restrict msg = msg_; - auto char *restrict new; + const unsigned char *restrict msg = msg_; + auto unsigned char *restrict new; register long int rr = state->r >> 3; auto size_t suffix_len = suffix ? __builtin_strlen(suffix) : 0; register size_t ext; @@ -453,12 +448,12 @@ libkeccak_fast_digest(struct libkeccak_state *restrict state, const void *restri state->mptr += msglen; if (bits) - state->M[state->mptr] = msg[msglen] & (char)((1 << bits) - 1); + state->M[state->mptr] = msg[msglen] & (unsigned char)((1 << bits) - 1); if (__builtin_expect(!!suffix_len, 1)) { - if (bits == 0) + if (!bits) state->M[state->mptr] = 0; while (suffix_len--) { - state->M[state->mptr] |= (char)((*suffix++ & 1) << bits++); + state->M[state->mptr] |= (unsigned char)((*suffix++ & 1) << bits++); if (bits == 8) bits = 0, state->M[++(state->mptr)] = 0; } @@ -496,8 +491,8 @@ int libkeccak_digest(struct libkeccak_state *restrict state, const void *restrict msg_, size_t msglen, size_t bits, const char *restrict suffix, void *restrict hashsum) { - const char *restrict msg = msg_; - auto char *restrict new; + const unsigned char *restrict msg = msg_; + auto unsigned char *restrict new; register long int rr = state->r >> 3; auto size_t suffix_len = suffix ? __builtin_strlen(suffix) : 0; register size_t ext; @@ -524,12 +519,12 @@ libkeccak_digest(struct libkeccak_state *restrict state, const void *restrict ms state->mptr += msglen; if (bits) - state->M[state->mptr] = msg[msglen] & (char)((1 << bits) - 1); + state->M[state->mptr] = msg[msglen] & (unsigned char)((1 << bits) - 1); if (__builtin_expect(!!suffix_len, 1)) { - if (bits == 0) + if (!bits) state->M[state->mptr] = 0; while (suffix_len--) { - state->M[state->mptr] |= (char)((*suffix++ & 1) << bits++); + state->M[state->mptr] |= (unsigned char)((*suffix++ & 1) << bits++); if (bits == 8) bits = 0, state->M[++(state->mptr)] = 0; } diff --git a/libkeccak.h b/libkeccak.h index b53d158..8f1193c 100644 --- a/libkeccak.h +++ b/libkeccak.h @@ -269,7 +269,7 @@ struct libkeccak_state { /** * Left over water to fill the sponge with at next update */ - char *M; + unsigned char *M; }; @@ -796,12 +796,12 @@ struct libkeccak_hmac_state { /** * The key right-padded and XOR:ed with the outer pad */ - char *restrict key_opad; + unsigned char *restrict key_opad; /** * The key right-padded and XOR:ed with the inner pad */ - char *restrict key_ipad; + unsigned char *restrict key_ipad; /* Not marshalled, implicitly unmarshalled using `key_opad`. */ /* Shares allocation with `key_opad`, do not `free`. */ @@ -819,7 +819,7 @@ struct libkeccak_hmac_state { * Buffer used to temporarily store bit shift message if * `.key_length` is not zero modulus 8 */ - char *restrict buffer; + unsigned char *restrict buffer; /** * The allocation size of `.buffer` @@ -829,7 +829,7 @@ struct libkeccak_hmac_state { /** * Part of feed key, message or digest that have not been passed yet */ - char leftover; + unsigned char leftover; char __pad[sizeof(void *) / sizeof(char) - 1]; }; @@ -1043,14 +1043,14 @@ LIBKECCAK_GCC_ONLY(__attribute__((__nonnull__, __nothrow__))) static inline size_t libkeccak_hmac_marshal(const struct libkeccak_hmac_state *restrict state, void *restrict data_) { - char *restrict data = data_; + unsigned char *restrict data = data_; size_t written = libkeccak_state_marshal(&state->sponge, data); data += written / sizeof(char); *(size_t *)data = state->key_length; data += sizeof(size_t) / sizeof(char); memcpy(data, state->key_opad, (state->key_length + 7) >> 3); data += ((state->key_length + 7) >> 3) / sizeof(char); - data[0] = (char)!!state->key_ipad; + data[0] = (unsigned char)!!state->key_ipad; data[1] = state->leftover; return written + sizeof(size_t) + ((state->key_length + 7) >> 3) + 2 * sizeof(char); } diff --git a/libkeccak_behex_lower.c b/libkeccak_behex_lower.c index 77e48c1..5098ecf 100644 --- a/libkeccak_behex_lower.c +++ b/libkeccak_behex_lower.c @@ -13,6 +13,7 @@ void libkeccak_behex_lower(char *restrict output, const void *restrict hashsum_, size_t n) { const unsigned char *restrict hashsum = hashsum_; + output[2 * n] = '\0'; while (n--) { output[2 * n + 0] = "0123456789abcdef"[(hashsum[n] >> 4) & 15]; diff --git a/libkeccak_behex_upper.c b/libkeccak_behex_upper.c index 73a67a1..b581f58 100644 --- a/libkeccak_behex_upper.c +++ b/libkeccak_behex_upper.c @@ -13,6 +13,7 @@ void libkeccak_behex_upper(char *restrict output, const void *restrict hashsum_, size_t n) { const unsigned char *restrict hashsum = hashsum_; + output[2 * n] = '\0'; while (n--) { output[2 * n + 0] = "0123456789ABCDEF"[(hashsum[n] >> 4) & 15]; diff --git a/libkeccak_degeneralise_spec.c b/libkeccak_degeneralise_spec.c index ec3eb56..ad5d415 100644 --- a/libkeccak_degeneralise_spec.c +++ b/libkeccak_degeneralise_spec.c @@ -7,8 +7,6 @@ # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif -#define have(v) (spec->v != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC) -#define copy(v) (v = spec->v) #define deft(v, dv) (have_##v ? v : (dv)) @@ -27,24 +25,29 @@ libkeccak_degeneralise_spec(struct libkeccak_generalised_spec *restrict spec, struct libkeccak_spec *restrict output_spec) { long int state_size, word_size, capacity, bitrate, output; - const int have_state_size = have(state_size); - const int have_word_size = have(word_size); - const int have_capacity = have(capacity); - const int have_bitrate = have(bitrate); - const int have_output = have(output); + const int have_state_size = spec->state_size != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; + const int have_word_size = spec->word_size != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; + const int have_capacity = spec->capacity != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; + const int have_bitrate = spec->bitrate != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; + const int have_output = spec->output != LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; if (have_state_size) { - copy(state_size); - if (state_size <= 0) return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_NONPOSITIVE; - if (state_size > 1600) return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_TOO_LARGE; - if (state_size % 25) return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_MOD_25; + state_size = spec->state_size; + if (state_size <= 0) + return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_NONPOSITIVE; + if (state_size > 1600) + return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_TOO_LARGE; + if (state_size % 25) + return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_MOD_25; } if (have_word_size) { - copy(word_size); - if (word_size <= 0) return LIBKECCAK_GENERALISED_SPEC_ERROR_WORD_NONPOSITIVE; - if (word_size > 64) return LIBKECCAK_GENERALISED_SPEC_ERROR_WORD_TOO_LARGE; + word_size = spec->word_size; + if (word_size <= 0) + return LIBKECCAK_GENERALISED_SPEC_ERROR_WORD_NONPOSITIVE; + if (word_size > 64) + return LIBKECCAK_GENERALISED_SPEC_ERROR_WORD_TOO_LARGE; if (have_state_size && state_size != word_size * 25) return LIBKECCAK_GENERALISED_SPEC_ERROR_STATE_WORD_INCOHERENCY; else if (!have_state_size) @@ -52,20 +55,25 @@ libkeccak_degeneralise_spec(struct libkeccak_generalised_spec *restrict spec, } if (have_capacity) { - copy(capacity); - if (capacity <= 0) return LIBKECCAK_GENERALISED_SPEC_ERROR_CAPACITY_NONPOSITIVE; - if (capacity & 7) return LIBKECCAK_GENERALISED_SPEC_ERROR_CAPACITY_MOD_8; + capacity = spec->capacity; + if (capacity <= 0) + return LIBKECCAK_GENERALISED_SPEC_ERROR_CAPACITY_NONPOSITIVE; + if (capacity & 7) + return LIBKECCAK_GENERALISED_SPEC_ERROR_CAPACITY_MOD_8; } if (have_bitrate) { - copy(bitrate); - if (bitrate <= 0) return LIBKECCAK_GENERALISED_SPEC_ERROR_BITRATE_NONPOSITIVE; - if (bitrate & 7) return LIBKECCAK_GENERALISED_SPEC_ERROR_BITRATE_MOD_8; + bitrate = spec->bitrate; + if (bitrate <= 0) + return LIBKECCAK_GENERALISED_SPEC_ERROR_BITRATE_NONPOSITIVE; + if (bitrate & 7) + return LIBKECCAK_GENERALISED_SPEC_ERROR_BITRATE_MOD_8; } if (have_output) { - copy(output); - if (output <= 0) return LIBKECCAK_GENERALISED_SPEC_ERROR_OUTPUT_NONPOSITIVE; + output = spec->output; + if (output <= 0) + return LIBKECCAK_GENERALISED_SPEC_ERROR_OUTPUT_NONPOSITIVE; } @@ -103,8 +111,6 @@ libkeccak_degeneralise_spec(struct libkeccak_generalised_spec *restrict spec, #undef deft -#undef copy -#undef have #ifdef __GNUC__ # pragma GCC diagnostic pop diff --git a/libkeccak_generalised_sum_fd.c b/libkeccak_generalised_sum_fd.c index 634d2c2..7c4df54 100644 --- a/libkeccak_generalised_sum_fd.c +++ b/libkeccak_generalised_sum_fd.c @@ -21,26 +21,26 @@ libkeccak_generalised_sum_fd(int fd, struct libkeccak_state *restrict state, con ssize_t got; struct stat attr; size_t blksize = 4096; - char *restrict chunk; + void *restrict chunk; if (libkeccak_state_initialise(state, spec) < 0) return -1; if (fstat(fd, &attr) == 0) if (attr.st_blksize > 0) - blksize = (size_t)(attr.st_blksize); + blksize = (size_t)attr.st_blksize; chunk = alloca(blksize); for (;;) { got = read(fd, chunk, blksize); - if (got < 0) { + if (got <= 0) { + if (!got) + break; if (errno == EINTR) continue; return -1; } - if (got == 0) - break; if (libkeccak_fast_update(state, chunk, (size_t)got) < 0) return -1; } diff --git a/libkeccak_hmac_copy.c b/libkeccak_hmac_copy.c index 0e389c1..9c52328 100644 --- a/libkeccak_hmac_copy.c +++ b/libkeccak_hmac_copy.c @@ -25,7 +25,7 @@ libkeccak_hmac_copy(struct libkeccak_hmac_state *restrict dest, const struct lib size = (src->key_length + 7) >> 3; dest->key_opad = malloc(2 * size); - if (dest->key_opad == NULL) { + if (!dest->key_opad) { libkeccak_state_destroy(&dest->sponge); return -1; } diff --git a/libkeccak_hmac_digest.c b/libkeccak_hmac_digest.c index aebb80b..76f21f6 100644 --- a/libkeccak_hmac_digest.c +++ b/libkeccak_hmac_digest.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#define NEED_EXPLICIT_BZERO 1 #include "common.h" @@ -20,10 +21,10 @@ int libkeccak_hmac_digest(struct libkeccak_hmac_state *restrict state, const void *restrict msg_, size_t msglen, size_t bits, const char *restrict suffix, void *restrict hashsum) { - const char *restrict msg = msg_; + const unsigned char *restrict msg = msg_; size_t hashsize = (size_t)(state->sponge.n >> 3); char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char)); - char leftover[2]; + unsigned char leftover[2]; size_t newlen; if (!tmp) @@ -39,8 +40,8 @@ libkeccak_hmac_digest(struct libkeccak_hmac_state *restrict state, const void *r goto fail; leftover[0] = state->leftover; if (bits) { - leftover[0] |= (char)(msg[msglen] >> (state->key_length & 7)); - leftover[1] = (char)((unsigned char)msg[msglen] << (8 - (state->key_length & 7))); + leftover[0] |= (unsigned char)(msg[msglen] >> (state->key_length & 7)); + leftover[1] = (unsigned char)(msg[msglen] << (8 - (state->key_length & 7))); } newlen = (state->key_length & 7) + bits; if (libkeccak_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) @@ -62,8 +63,8 @@ stage_2: goto fail; leftover[0] = state->leftover; if (bits) { - leftover[0] |= (char)(tmp[hashsize] >> (state->key_length & 7)); - leftover[1] = (char)((unsigned char)tmp[hashsize] << (8 - (state->key_length & 7))); + leftover[0] |= (unsigned char)(tmp[hashsize] >> (state->key_length & 7)); + leftover[1] = (unsigned char)(tmp[hashsize] << (8 - (state->key_length & 7))); } newlen = (state->key_length & 7) + bits; if (libkeccak_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) diff --git a/libkeccak_hmac_fast_digest.c b/libkeccak_hmac_fast_digest.c index c203716..d8b4509 100644 --- a/libkeccak_hmac_fast_digest.c +++ b/libkeccak_hmac_fast_digest.c @@ -20,10 +20,10 @@ int libkeccak_hmac_fast_digest(struct libkeccak_hmac_state *restrict state, const void *restrict msg_, size_t msglen, size_t bits, const char *restrict suffix, void *restrict hashsum) { - const char *restrict msg = msg_; + const unsigned char *restrict msg = msg_; size_t hashsize = (size_t)state->sponge.n >> 3; - char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char)); - char leftover[2]; + unsigned char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char)); + unsigned char leftover[2]; size_t newlen; if (!tmp) @@ -39,8 +39,8 @@ libkeccak_hmac_fast_digest(struct libkeccak_hmac_state *restrict state, const vo goto fail; leftover[0] = state->leftover; if (bits) { - leftover[0] |= (char)(msg[msglen] >> (state->key_length & 7)); - leftover[1] = (char)((unsigned char)msg[msglen] << (8 - (state->key_length & 7))); + leftover[0] |= (unsigned char)(msg[msglen] >> (state->key_length & 7)); + leftover[1] = (unsigned char)(msg[msglen] << (8 - (state->key_length & 7))); } newlen = (state->key_length & 7) + bits; if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) @@ -62,8 +62,8 @@ stage_2: goto fail; leftover[0] = state->leftover; if (bits) { - leftover[0] |= (char)(tmp[hashsize] >> (state->key_length & 7)); - leftover[1] = (char)((unsigned char)tmp[hashsize] << (8 - (state->key_length & 7))); + leftover[0] |= (unsigned char)(tmp[hashsize] >> (state->key_length & 7)); + leftover[1] = (unsigned char)(tmp[hashsize] << (8 - (state->key_length & 7))); } newlen = (state->key_length & 7) + bits; if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0) diff --git a/libkeccak_hmac_fast_update.c b/libkeccak_hmac_fast_update.c index fe49960..5a1eb70 100644 --- a/libkeccak_hmac_fast_update.c +++ b/libkeccak_hmac_fast_update.c @@ -14,8 +14,8 @@ int libkeccak_hmac_fast_update(struct libkeccak_hmac_state *restrict state, const void *restrict msg_, size_t msglen) { - const char *restrict msg = msg_; - char *old; + const unsigned char *restrict msg = msg_; + unsigned char *old; size_t i; int n, cn; @@ -43,9 +43,9 @@ libkeccak_hmac_fast_update(struct libkeccak_hmac_state *restrict state, const vo n = (int)(state->key_length & 7); cn = 8 - n; for (i = 1; i < msglen; i++) - state->buffer[i] = (char)((msg[i - 1] >> cn) | (msg[i] << n)); - state->buffer[0] = (char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n)); - state->leftover = (char)((unsigned char)msg[msglen - 1] >> cn); + state->buffer[i] = (unsigned char)((msg[i - 1] >> cn) | (msg[i] << n)); + state->buffer[0] = (unsigned char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n)); + state->leftover = (unsigned char)(msg[msglen - 1] >> cn); return libkeccak_fast_update(&state->sponge, state->buffer, msglen); } diff --git a/libkeccak_hmac_set_key.c b/libkeccak_hmac_set_key.c index 89c01e6..f8f6a39 100644 --- a/libkeccak_hmac_set_key.c +++ b/libkeccak_hmac_set_key.c @@ -14,7 +14,7 @@ int libkeccak_hmac_set_key(struct libkeccak_hmac_state *restrict state, const void *restrict key, size_t key_length) { size_t i, size, new_key_length, key_bytes; - char *old; + unsigned char *old; size = (size_t)(state->sponge.r) > key_length ? (size_t)(state->sponge.r) : key_length; new_key_length = size; @@ -30,7 +30,7 @@ libkeccak_hmac_set_key(struct libkeccak_hmac_state *restrict state, const void * memcpy(state->key_opad, key, key_bytes); if (key_length & 7) - state->key_opad[(key_bytes >> 3) - 1] &= (char)((1 << (key_length & 7)) - 1); + state->key_opad[(key_bytes >> 3) - 1] &= (unsigned char)((1 << (key_length & 7)) - 1); if ((size_t)(state->sponge.r) > key_length) __builtin_memset(state->key_opad + key_bytes / sizeof(char), 0, size - key_bytes); diff --git a/libkeccak_hmac_unmarshal.c b/libkeccak_hmac_unmarshal.c index 81d2121..9627f1e 100644 --- a/libkeccak_hmac_unmarshal.c +++ b/libkeccak_hmac_unmarshal.c @@ -12,7 +12,7 @@ size_t libkeccak_hmac_unmarshal(struct libkeccak_hmac_state *restrict state, const void *restrict data_) { - const char *restrict data = data_; + const unsigned char *restrict data = data_; size_t parsed, size, i; state->key_opad = NULL; diff --git a/libkeccak_hmac_update.c b/libkeccak_hmac_update.c index 6182a0a..535b099 100644 --- a/libkeccak_hmac_update.c +++ b/libkeccak_hmac_update.c @@ -1,4 +1,5 @@ /* See LICENSE file for copyright and license details. */ +#define NEED_EXPLICIT_BZERO 1 #include "common.h" @@ -14,7 +15,7 @@ int libkeccak_hmac_update(struct libkeccak_hmac_state *restrict state, const void *restrict msg_, size_t msglen) { - const char *restrict msg = msg_; + const unsigned char *restrict msg = msg_; size_t i; int n, cn, r; @@ -42,9 +43,9 @@ libkeccak_hmac_update(struct libkeccak_hmac_state *restrict state, const void *r n = (int)(state->key_length & 7); cn = 8 - n; for (i = 1; i < msglen; i++) - state->buffer[i] = (char)(((unsigned char)msg[i - 1] >> cn) | (msg[i] << n)); - state->buffer[0] = (char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n)); - state->leftover = (char)((unsigned char)msg[msglen - 1] >> cn); + state->buffer[i] = (unsigned char)((msg[i - 1] >> cn) | (msg[i] << n)); + state->buffer[0] = (unsigned char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n)); + state->leftover = (unsigned char)(msg[msglen - 1] >> cn); r = libkeccak_update(&state->sponge, state->buffer, msglen); my_explicit_bzero(state->buffer, msglen); diff --git a/libkeccak_hmac_wipe.c b/libkeccak_hmac_wipe.c index 77b4449..1f29b47 100644 --- a/libkeccak_hmac_wipe.c +++ b/libkeccak_hmac_wipe.c @@ -10,10 +10,12 @@ void libkeccak_hmac_wipe(volatile struct libkeccak_hmac_state *restrict state) { - volatile char *restrict key_pads; + volatile unsigned char *restrict key_pads; size_t i, size; + key_pads = state->key_opad; size = 2 * ((state->key_length + 7) >> 3); + libkeccak_state_wipe(&state->sponge); for (i = 0; i < size; i++) key_pads[i] = 0; diff --git a/libkeccak_state_initialise.c b/libkeccak_state_initialise.c index 390fb87..aa77051 100644 --- a/libkeccak_state_initialise.c +++ b/libkeccak_state_initialise.c @@ -13,21 +13,25 @@ int libkeccak_state_initialise(struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec) { long int x; + state->r = spec->bitrate; state->n = spec->output; state->c = spec->capacity; state->b = state->r + state->c; state->w = x = state->b / 25; state->l = 0; + if (x & 0xF0L) state->l |= 4, x >>= 4; if (x & 0x0CL) state->l |= 2, x >>= 2; if (x & 0x02L) state->l |= 1; + state->nr = 12 + (state->l << 1); state->wmod = (state->w == 64) ? ~0LL : (int64_t)((1ULL << state->w) - 1); for (x = 0; x < 25; x++) state->S[x] = 0; state->mptr = 0; state->mlen = (size_t)(state->r * state->b) >> 2; + state->M = malloc(state->mlen * sizeof(char)); return state->M == NULL ? -1 : 0; } diff --git a/libkeccak_state_marshal.c b/libkeccak_state_marshal.c index f5b086e..00b54cc 100644 --- a/libkeccak_state_marshal.c +++ b/libkeccak_state_marshal.c @@ -13,7 +13,7 @@ size_t libkeccak_state_marshal(const struct libkeccak_state *restrict state, void *restrict data_) { #define set(type, var) *((type *)data) = state->var, data += sizeof(type) / sizeof(char) - char *restrict data = data_; + unsigned char *restrict data = data_; set(long int, r); set(long int, c); set(long int, n); diff --git a/libkeccak_state_unmarshal.c b/libkeccak_state_unmarshal.c index ca2013a..af1f7de 100644 --- a/libkeccak_state_unmarshal.c +++ b/libkeccak_state_unmarshal.c @@ -13,7 +13,7 @@ size_t libkeccak_state_unmarshal(struct libkeccak_state *restrict state, const void *restrict data_) { #define get(type, var) state->var = *((const type *)data), data += sizeof(type) / sizeof(char) - const char *restrict data = data_; + const unsigned char *restrict data = data_; get(long int, r); get(long int, c); get(long int, n); diff --git a/libkeccak_state_unmarshal_skip.c b/libkeccak_state_unmarshal_skip.c index b8c244c..34a898a 100644 --- a/libkeccak_state_unmarshal_skip.c +++ b/libkeccak_state_unmarshal_skip.c @@ -12,7 +12,7 @@ size_t libkeccak_state_unmarshal_skip(const void *restrict data_) { - const char *restrict data = data_; + const unsigned char *restrict data = data_; data += (7 * sizeof(long int) + 26 * sizeof(int64_t)) / sizeof(char); return sizeof(struct libkeccak_state) - sizeof(char *) + *(const size_t *)data * sizeof(char); } diff --git a/libkeccak_state_wipe_message.c b/libkeccak_state_wipe_message.c index 115320e..026bc8e 100644 --- a/libkeccak_state_wipe_message.c +++ b/libkeccak_state_wipe_message.c @@ -10,8 +10,9 @@ void libkeccak_state_wipe_message(volatile struct libkeccak_state *restrict state) { - volatile char *restrict M = state->M; + volatile unsigned char *restrict M = state->M; size_t i; + for (i = 0; i < state->mptr; i++) M[i] = 0; } diff --git a/libkeccak_state_wipe_sponge.c b/libkeccak_state_wipe_sponge.c index eb564c0..14a203b 100644 --- a/libkeccak_state_wipe_sponge.c +++ b/libkeccak_state_wipe_sponge.c @@ -12,6 +12,7 @@ libkeccak_state_wipe_sponge(volatile struct libkeccak_state *restrict state) { volatile int64_t *restrict S = state->S; size_t i; + for (i = 0; i < 25; i++) S[i] = 0; } diff --git a/libkeccak_unhex.c b/libkeccak_unhex.c index a12beb1..00bb039 100644 --- a/libkeccak_unhex.c +++ b/libkeccak_unhex.c @@ -15,6 +15,7 @@ libkeccak_unhex(void *restrict output_, const char *restrict hashsum) unsigned char *restrict output = output_; size_t n = strlen(hashsum) / 2; unsigned char a, b; + while (n--) { a = (unsigned char)hashsum[2 * n + 0]; b = (unsigned char)hashsum[2 * n + 1]; @@ -22,7 +23,7 @@ libkeccak_unhex(void *restrict output_, const char *restrict hashsum) a = (unsigned char)((a & 15) + (a > '9' ? 9 : 0)); b = (unsigned char)((b & 15) + (b > '9' ? 9 : 0)); - a <<= 4; + a = (unsigned char)(a << 4); a |= b; output[n] = a; } @@ -21,32 +21,40 @@ test_hex(void) char hextest[2 * 8 + 1]; printf("Testing libkeccak_behex_lower: "); - libkeccak_behex_lower(hextest, (const char*)bindata, 8); - if (!strcmp(hextest, hexdata_lower)) + libkeccak_behex_lower(hextest, (const char *)bindata, 8); + if (!strcmp(hextest, hexdata_lower)) { printf("OK\n"); - else - return printf("Fail\n"), -1; + } else { + printf("Fail\n"); + return -1; + } printf("Testing libkeccak_behex_upper: "); - libkeccak_behex_upper(hextest, (const char*)bindata, 8); - if (!strcmp(hextest, hexdata_upper)) + libkeccak_behex_upper(hextest, (const char *)bindata, 8); + if (!strcmp(hextest, hexdata_upper)) { printf("OK\n"); - else - return printf("Fail\n"), -1; + } else { + printf("Fail\n"); + return -1; + } printf("Testing libkeccak_unhex on uppercase: "); libkeccak_unhex(hextest, hexdata_upper); - if (!memcmp(bindata, hextest, 8 * sizeof(char))) + if (!memcmp(bindata, hextest, 8 * sizeof(char))) { printf("OK\n"); - else - return printf("Fail\n"), -1; + } else { + printf("Fail\n"); + return -1; + } printf("Testing libkeccak_unhex on lowercase: "); libkeccak_unhex(hextest, hexdata_lower); - if (!memcmp(bindata, hextest, 8 * sizeof(char))) + if (!memcmp(bindata, hextest, 8 * sizeof(char))) { printf("OK\n"); - else - return printf("Fail\n"), -1; + } else { + printf("Fail\n"); + return -1; + } printf("\n"); return 0; @@ -67,42 +75,66 @@ test_state(struct libkeccak_spec *restrict spec) size_t marshal_size, marshalled_size, i, n; char *restrict marshalled_data; - if (state = libkeccak_state_create(spec), state == NULL) - return perror("libkeccak_state_initialise"), -1; + state = libkeccak_state_create(spec); + if (!state) { + perror("libkeccak_state_initialise"); + return -1; + } n = state->mlen / 2; for (i = 0; i < n; i++) - state->M[state->mptr++] = (char)(i & 255); + state->M[state->mptr++] = (unsigned char)i; - if (state2 = libkeccak_state_duplicate(state), state2 == NULL) - return perror("libkeccak_state_duplicate"), -1; + state2 = libkeccak_state_duplicate(state); + if (!state2) { + perror("libkeccak_state_duplicate"); + return -1; + } - if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1]) - return printf("Inconsistency found between original state and duplicate state.\n"), -1; + if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1]) { + printf("Inconsistency found between original state and duplicate state.\n"); + return -1; + } marshal_size = libkeccak_state_marshal_size(state2); - if (marshalled_data = malloc(marshal_size), marshalled_data == NULL) - return perror("malloc"), -1; + marshalled_data = malloc(marshal_size); + if (!marshalled_data) { + perror("malloc"); + return -1; + } marshalled_size = libkeccak_state_marshal(state2, marshalled_data); - if (marshalled_size != marshal_size) - return printf("libkeccak_state_marshal returned an unexpected value.\n"), -1; + if (marshalled_size != marshal_size) { + printf("libkeccak_state_marshal returned an unexpected value.\n"); + return -1; + } libkeccak_state_free(state); - if (state = malloc(sizeof(struct libkeccak_state)), state == NULL) - return perror("malloc"), -1; + state = malloc(sizeof(struct libkeccak_state)); + if (!state) { + perror("malloc"); + return -1; + } marshalled_size = libkeccak_state_unmarshal(state, marshalled_data); - if (marshalled_size == 0) - return perror("libkeccak_state_unmarshal"), -1; - if (marshalled_size != marshal_size) - return printf("libkeccak_state_unmarshal returned an unexpected value.\n"), -1; + if (!marshalled_size) { + perror("libkeccak_state_unmarshal"); + return -1; + } + if (marshalled_size != marshal_size) { + printf("libkeccak_state_unmarshal returned an unexpected value.\n"); + return -1; + } - if (libkeccak_state_unmarshal_skip(marshalled_data) != marshal_size) - return printf("libkeccak_state_unmarshal_skip returned an unexpected value.\n"), -1; + if (libkeccak_state_unmarshal_skip(marshalled_data) != marshal_size) { + printf("libkeccak_state_unmarshal_skip returned an unexpected value.\n"); + return -1; + } - if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1]) - return printf("Inconsistency found between original state and unmarshalled state.\n"), -1; + if (state->M[state->mptr - 1] != state2->M[state2->mptr - 1]) { + printf("Inconsistency found between original state and unmarshalled state.\n"); + return -1; + } free(marshalled_data); libkeccak_state_free(state); @@ -123,10 +155,10 @@ test_state(struct libkeccak_spec *restrict spec) */ static int test_digest_case(const struct libkeccak_spec *restrict spec, const char *restrict suffix, - const char *restrict msg, long bits, const char *restrict expected_answer) + const char *restrict msg, long int bits, const char *restrict expected_answer) { struct libkeccak_state state; - char *restrict hashsum; + unsigned char *restrict hashsum; char *restrict hexsum; int ok; @@ -167,7 +199,8 @@ test_digest_case(const struct libkeccak_spec *restrict spec, const char *restric * * @return Zero on success, -1 on error */ -static int test_digest(void) +static int +test_digest(void) { #define sha3(output, message)\ (printf(" Testing SHA3-"#output"(%s): ", #message),\ @@ -212,96 +245,123 @@ static int test_digest(void) answer = "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"; - if (sha3(224, "")) return -1; + if (sha3(224, "")) + return -1; answer = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"; - if (sha3(256, "")) return -1; + if (sha3(256, "")) + return -1; answer = "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004"; - if (sha3(384, "")) return -1; + if (sha3(384, "")) + return -1; answer = "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a6" - "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"; - if (sha3(512, "")) return -1; + "15b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"; + if (sha3(512, "")) + return -1; answer = "f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd"; - if (keccak(224, "")) return -1; + if (keccak(224, "")) + return -1; answer = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; - if (keccak(256, "")) return -1; + if (keccak(256, "")) + return -1; answer = "2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff"; - if (keccak(384, "")) return -1; + if (keccak(384, "")) + return -1; answer = "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304" - "c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e"; - if (keccak(512, "")) return -1; + "c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e"; + if (keccak(512, "")) + return -1; answer = "22c8017ac8bcf65f59d1b7e92c9d4c6739d25e34ce5cb608b24ff096"; - if (sha3(224, "withdrew hypothesis snakebird qmc2")) return -1; + if (sha3(224, "withdrew hypothesis snakebird qmc2")) + return -1; answer = "43808dde2662143dc4eed5dac5e98c74b06711829f02a3b121bd74f3"; - if (sha3(224, "intensifierat sturdiness perl-image-exiftool vingla")) return -1; + if (sha3(224, "intensifierat sturdiness perl-image-exiftool vingla")) + return -1; answer = "d32b4ac86065774dee5eb5cdd2f67b4e86501086d7373884e8b20a36"; - if (sha3(224, "timjan avogadro uppdriven lib32-llvm-amdgpu-snapshot")) return -1; + if (sha3(224, "timjan avogadro uppdriven lib32-llvm-amdgpu-snapshot")) + return -1; answer = "efbd76d45bfa952485148f8ad46143897f17c27ffdc8eb7287f9353b"; - if (sha3(224, "grilo-plugins auditorium tull dissimilarity's")) return -1; + if (sha3(224, "grilo-plugins auditorium tull dissimilarity's")) + return -1; answer = "6705aa36ecf58f333e0e6364ac1d0b7931d402e13282127cfd6f876c"; - if (sha3(224, "royalty tt yellowstone deficiencies")) return -1; + if (sha3(224, "royalty tt yellowstone deficiencies")) + return -1; answer = "803a0ff09dda0df306e483a9f91b20a3dbbf9c2ebb8d0a3b28f3b9e0"; - if (sha3(224, "kdegames-kdiamond tunisisk occurrence's outtalad")) return -1; + if (sha3(224, "kdegames-kdiamond tunisisk occurrence's outtalad")) + return -1; answer = "a64779aca943a6aef1d2e7c9a0f4e997f4dabd1f77112a22121d3ed5"; - if (sha3(224, "chevalier slat's spindel representations")) return -1; + if (sha3(224, "chevalier slat's spindel representations")) + return -1; answer = "f0a3e0587af7723f0aa4719059d3f5107115a5b3667cd5209cc4d867"; - if (sha3(224, "archery lexicographical equine veered")) return -1; + if (sha3(224, "archery lexicographical equine veered")) + return -1; answer = "312e7e3c6403ab1a086155fb9a52b22a3d0d257876afd2b93fb7272e"; - if (sha3(224, "splay washbasin opposing there")) return -1; + if (sha3(224, "splay washbasin opposing there")) + return -1; answer = "270ba05b764221ff5b5d94adfb4fdb1f36f07fe7c438904a5f3df071"; - if (sha3(224, "faktum desist thundered klen")) return -1; + if (sha3(224, "faktum desist thundered klen")) + return -1; answer = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; - if (keccak_bits(256, "\x00", 0)) return -1; + if (keccak_bits(256, "\x00", 0)) + return -1; answer = "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"; - if (keccak_bits(256, "\x02", 2)) return -1; + if (keccak_bits(256, "\x02", 2)) + return -1; answer = "3a1108d4a90a31b85a10bdce77f4bfbdcc5b1d70dd405686f8bbde834aa1a410"; - if (keccak_bits(256, "\x03", 2)) return -1; + if (keccak_bits(256, "\x03", 2)) + return -1; answer = "46b9dd2b0ba88d13233b3feb743eeb243fcd52ea62b81b82b50c27646ed5762f"; - if (keccak_bits(256, "\x0F", 4)) return -1; + if (keccak_bits(256, "\x0F", 4)) + return -1; answer = "3a1108d4a90a31b85a10bdce77f4bfbd"; - if (rawshake(256, 128, "")) return -1; + if (rawshake(256, 128, "")) + return -1; answer = "46b9dd2b0ba88d13233b3feb743eeb24"; - if (rawshake_bits(256, 128, "\x03", 2)) return -1; + if (rawshake_bits(256, 128, "\x03", 2)) + return -1; answer = "46b9dd2b0ba88d13233b3feb743eeb24"; - if (shake(256, 128, "")) return -1; + if (shake(256, 128, "")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de92253515713cce8" "d2098be1c82df40b40e375549c0eeb655f92d718f01f147ba1c7c67844c7ba8b11492cd6"; - if (keccak_g(1024, 1600 - 1024, 576, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 576, "capitol's kvistfri broadly raping")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de92253515713cce8" "d2098be1c82df40b40e375549c0eeb655f92d718f01f147ba1c7c67844c7ba8b11492cd6" "143466958504c110522f772fe746573b1dc905f943ed1ec6ecf858575798596beeca4eb6" "bb7bea635bcea6331315728fb57866370bf1ad5d"; - if (keccak_g(1024, 1600 - 1024, 1024, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 1024, "capitol's kvistfri broadly raping")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de92253515713cce8" "d2098be1c82df40b40e375549c0eeb655f92d718f01f147ba1c7c67844c7ba8b11492cd6" @@ -309,7 +369,8 @@ static int test_digest(void) "bb7bea635bcea6331315728fb57866370bf1ad5decbc56d28d47ce53f18376d9f5531551" "7a976d52dd3f98b7025e0b3c513c6d17d40462cddb5406d693bbe859a136af5375b5dd6e" "3478934b00aa6cd44aa7ae2cd0271d83fbab699b"; - if (keccak_g(1024, 1600 - 1024, 1600, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 1600, "capitol's kvistfri broadly raping")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de92253515713cce8" "d2098be1c82df40b40e375549c0eeb655f92d718f01f147ba1c7c67844c7ba8b11492cd6" @@ -317,7 +378,8 @@ static int test_digest(void) "bb7bea635bcea6331315728fb57866370bf1ad5decbc56d28d47ce53f18376d9f5531551" "7a976d52dd3f98b7025e0b3c513c6d17d40462cddb5406d693bbe859a136af5375b5dd6e" "3478934b00aa6cd44aa7ae2cd0271d83fbab699b9c"; - if (keccak_g(1024, 1600 - 1024, 1608, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 1608, "capitol's kvistfri broadly raping")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de92253515713cce8" "d2098be1c82df40b40e375549c0eeb655f92d718f01f147ba1c7c67844c7ba8b11492cd6" @@ -331,34 +393,44 @@ static int test_digest(void) "a9f9ae4232f313740b4fb787545dc19e7778f7082b3fa5824d2400c012be1a6c5ade7149" "e452d310752fa9ebb964ab36fde0c8f46f47a0e2c9b20f24e3cca904bbedaa7ea176f662" "33cd2d95"; - if (keccak_g(1024, 1600 - 1024, 3200, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 3200, "capitol's kvistfri broadly raping")) + return -1; answer = "65070cdd6f91c0aadcfc470895a2606c828bce7ce3fa723418c9013de9225351"; - if (keccak_g(1024, 1600 - 1024, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(1024, 1600 - 1024, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "e6f86ebc15b962f73f36f36fc8a84c3ae84b1c1023bfd4c5f1829389135aecc3"; - if (keccak_g(512, 1600 - 512, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(512, 1600 - 512, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "420b97fc88962c87ec2adaa8f48d74d9ff4ea7ae7d691f9c33b8713ca1d3d573"; - if (keccak_g(256, 1600 - 256, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(256, 1600 - 256, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "524790afbe4706d938b6f753e14104f556890e2a415e211b0564d60499db0333"; - if (keccak_g(512, 800 - 512, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(512, 800 - 512, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "04a6b4ad08b3018eefba0fb756272d949ac0f71c26f836d31dd13b28b884aa0f"; - if (keccak_g(256, 800 - 256, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(256, 800 - 256, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "d56f547791225e54460e6274ed31e57b7085820c11d65f1f322a16a3352c85ed"; - if (keccak_g(256, 400 - 256, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(256, 400 - 256, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "ceec066a57b9b31a5a0661df7bafec4183a26d0ed81e50bc958471f84fa347a7"; - if (keccak_g(128, 400 - 128, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(128, 400 - 128, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "b18f679c7105a72a993f70fa5adb3f17ef7ccffaffb4dc0f6fed74aa2f565194"; - if (keccak_g(128, 200 - 128, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(128, 200 - 128, 256, "capitol's kvistfri broadly raping")) + return -1; answer = "9b845c1ecc2b1b3a48ba42ef29ccc4b348da8ab15074a870d8e799ca33c15e4b"; - if (keccak_g(64, 200 - 64, 256, "capitol's kvistfri broadly raping")) return -1; + if (keccak_g(64, 200 - 64, 256, "capitol's kvistfri broadly raping")) + return -1; printf("\n"); @@ -388,7 +460,7 @@ test_update_case(const struct libkeccak_spec *restrict spec, const char *restric const char *restrict msg, const char *restrict expected_answer) { struct libkeccak_state state; - char *restrict hashsum; + unsigned char *restrict hashsum; char *restrict hexsum; int ok; @@ -435,7 +507,8 @@ test_update_case(const struct libkeccak_spec *restrict spec, const char *restric * * @return Zero on success, -1 on error */ -static int test_update(void) +static int +test_update(void) { #define sha3(output, message)\ (printf(" Testing SHA3-"#output"(%s): ", #message),\ @@ -443,40 +516,50 @@ static int test_update(void) test_update_case(&spec, LIBKECCAK_SHA3_SUFFIX, message, answer)) struct libkeccak_spec spec; - const char* answer; + const char *answer; printf("Testing libkeccak_update:\n"); answer = "22c8017ac8bcf65f59d1b7e92c9d4c6739d25e34ce5cb608b24ff096"; - if (sha3(224, "withdrew hypothesis snakebird qmc2")) return -1; + if (sha3(224, "withdrew hypothesis snakebird qmc2")) + return -1; answer = "43808dde2662143dc4eed5dac5e98c74b06711829f02a3b121bd74f3"; - if (sha3(224, "intensifierat sturdiness perl-image-exiftool vingla")) return -1; + if (sha3(224, "intensifierat sturdiness perl-image-exiftool vingla")) + return -1; answer = "d32b4ac86065774dee5eb5cdd2f67b4e86501086d7373884e8b20a36"; - if (sha3(224, "timjan avogadro uppdriven lib32-llvm-amdgpu-snapshot")) return -1; + if (sha3(224, "timjan avogadro uppdriven lib32-llvm-amdgpu-snapshot")) + return -1; answer = "efbd76d45bfa952485148f8ad46143897f17c27ffdc8eb7287f9353b"; - if (sha3(224, "grilo-plugins auditorium tull dissimilarity's")) return -1; + if (sha3(224, "grilo-plugins auditorium tull dissimilarity's")) + return -1; answer = "6705aa36ecf58f333e0e6364ac1d0b7931d402e13282127cfd6f876c"; - if (sha3(224, "royalty tt yellowstone deficiencies")) return -1; + if (sha3(224, "royalty tt yellowstone deficiencies")) + return -1; answer = "803a0ff09dda0df306e483a9f91b20a3dbbf9c2ebb8d0a3b28f3b9e0"; - if (sha3(224, "kdegames-kdiamond tunisisk occurrence's outtalad")) return -1; + if (sha3(224, "kdegames-kdiamond tunisisk occurrence's outtalad")) + return -1; answer = "a64779aca943a6aef1d2e7c9a0f4e997f4dabd1f77112a22121d3ed5"; - if (sha3(224, "chevalier slat's spindel representations")) return -1; + if (sha3(224, "chevalier slat's spindel representations")) + return -1; answer = "f0a3e0587af7723f0aa4719059d3f5107115a5b3667cd5209cc4d867"; - if (sha3(224, "archery lexicographical equine veered")) return -1; + if (sha3(224, "archery lexicographical equine veered")) + return -1; answer = "312e7e3c6403ab1a086155fb9a52b22a3d0d257876afd2b93fb7272e"; - if (sha3(224, "splay washbasin opposing there")) return -1; + if (sha3(224, "splay washbasin opposing there")) + return -1; answer = "270ba05b764221ff5b5d94adfb4fdb1f36f07fe7c438904a5f3df071"; - if (sha3(224, "faktum desist thundered klen")) return -1; + if (sha3(224, "faktum desist thundered klen")) + return -1; printf("\n"); @@ -499,17 +582,20 @@ static int test_update(void) * @param expected_answer The hashum we expect, must be in lowercase hexadecimal * @return Zero on success, -1 on error */ -static int test_squeeze_case(struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec, - long fast_squeezes, long squeezes, int fast_digest, char* restrict hashsum, - char *restrict hexsum, const char *restrict expected_answer) +static int +test_squeeze_case(struct libkeccak_state *restrict state, const struct libkeccak_spec *restrict spec, + long int fast_squeezes, long int squeezes, int fast_digest, void *restrict hashsum, + char *restrict hexsum, const char *restrict expected_answer) { #define message "withdrew hypothesis snakebird qmc2" - long i; + long int i; int ok; libkeccak_state_reset(state); - if (libkeccak_digest(state, message, strlen(message), 0, LIBKECCAK_SHA3_SUFFIX, fast_digest ? NULL : hashsum)) - return perror("libkeccak_digest"), -1; + if (libkeccak_digest(state, message, strlen(message), 0, LIBKECCAK_SHA3_SUFFIX, fast_digest ? NULL : hashsum)) { + perror("libkeccak_digest"); + return -1; + } libkeccak_fast_squeeze(state, fast_squeezes); for (i = fast_squeezes; i < squeezes; i++) @@ -544,7 +630,7 @@ test_squeeze(void) struct libkeccak_spec spec; struct libkeccak_state state; - char *restrict hashsum; + unsigned char *restrict hashsum; char *restrict hexsum; libkeccak_spec_sha3(&spec, 224); @@ -617,7 +703,7 @@ test_file(const struct libkeccak_spec *restrict spec, const char *restrict suffi const char *restrict filename, const char *restrict expected_answer) { struct libkeccak_state state; - char *restrict hashsum; + unsigned char *restrict hashsum; char *restrict hexsum; int ok, fd; @@ -674,8 +760,10 @@ main(void) struct libkeccak_spec spec; libkeccak_generalised_spec_initialise(&gspec); - if (libkeccak_degeneralise_spec(&gspec, &spec)) - return printf("libkeccak_degeneralise_spec failed with all members at automatic.\n"), 1; + if (libkeccak_degeneralise_spec(&gspec, &spec)) { + printf("libkeccak_degeneralise_spec failed with all members at automatic.\n"); + return 1; + } printf("Resolution of default specification:\n"); printf(" bitrate: %li\n", gspec.bitrate); @@ -683,18 +771,22 @@ main(void) printf(" output: %li\n", gspec.output); printf(" state size: %li\n", gspec.state_size); printf(" word size: %li\n", gspec.word_size); - if (gspec.word_size * 25 != gspec.state_size) return printf("Invalid information\n"), 1; - if (gspec.bitrate + gspec.capacity != gspec.state_size) return printf("Invalid information\n"), 1; - if (gspec.state_size != 1600) return printf("Incorrect information\n"), 1; - if (gspec.bitrate != gspec.output * 2) return printf("Incorrect information\n"), 1; - if (gspec.output != 512) return printf("Incorrect information\n"), 1; + + if (gspec.word_size * 25 != gspec.state_size || + gspec.bitrate + gspec.capacity != gspec.state_size) { + printf("Invalid information\n"); + return 1; + } + if (gspec.state_size != 1600 || + gspec.bitrate != gspec.output * 2 || + gspec.output != 512) { + printf("Incorrect information\n"); + return 1; + } printf("\n"); - if (test_hex()) return 1; - if (test_state(&spec)) return 1; - if (test_digest()) return 1; - if (test_update()) return 1; - if (test_squeeze()) return 1; + if (test_hex() || test_state(&spec) || test_digest() || test_update() || test_squeeze()) + return 1; if (test_file(&spec, LIBKECCAK_SHA3_SUFFIX, ".testfile", "a95484492e9ade0f1d28f872d197ff45d891e85e78f918643f41d524c5d6ab0f" |