From 7c862a92626e5663bb5e755107095a228a206967 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 4 Nov 2014 19:50:07 +0100 Subject: begin makefile + fix warnings and errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 13 +++++++++++++ src/libkeccak.h | 2 +- src/libkeccak/digest.c | 29 +++++++++++++++-------------- src/libkeccak/digest.h | 6 +++--- src/libkeccak/files.h | 10 +++++----- src/libkeccak/generalised-spec.c | 17 ++++++++++++----- src/libkeccak/generalised-spec.h | 12 ++++++------ src/libkeccak/hex.h | 2 +- src/libkeccak/spec.h | 8 ++++---- src/libkeccak/state.c | 12 ++++++------ src/libkeccak/state.h | 10 +++++----- 11 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3878841 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +FLAGS = -std=gnu99 + + +OBJ = digest files generalised-spec hex state + + +.PHONY: all +all: $(foreach O,$(OBJ),obj/libkeccak/$(O).o) + +obj/libkeccak/%.o: src/libkeccak/%.c src/libkeccak.h src/libkeccak/*.h + @mkdir -p obj/libkeccak + $(CC) $(FLAGS) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< + diff --git a/src/libkeccak.h b/src/libkeccak.h index fa564c4..ad64aff 100644 --- a/src/libkeccak.h +++ b/src/libkeccak.h @@ -28,5 +28,5 @@ #include "libkeccak/files.h" -#undef +#endif diff --git a/src/libkeccak/digest.c b/src/libkeccak/digest.c index cf17538..67301a6 100644 --- a/src/libkeccak/digest.c +++ b/src/libkeccak/digest.c @@ -90,7 +90,7 @@ static const int_fast64_t RC[] = * @param state The hashing state * @param rc The round contant for this round */ -static __attribute__((leaf, nonnull, nothrow, hot)) +static __attribute__((nonnull, nothrow, hot)) void libkeccak_f_round(libkeccak_state_t* restrict state, int_fast64_t rc) { int_fast64_t* restrict A = state->S; @@ -135,7 +135,7 @@ void libkeccak_f_round(libkeccak_state_t* restrict state, int_fast64_t rc) * @param state The hashing state * @param rc The round contant for this round */ -static __attribute__((leaf, nonnull, nothrow, hot)) +static __attribute__((nonnull, nothrow, hot)) void libkeccak_f_round64(libkeccak_state_t* restrict state, int_fast64_t rc) { int_fast64_t* restrict A = state->S; @@ -204,12 +204,12 @@ void libkeccak_f(libkeccak_state_t* restrict state) * @param off The offset in the message * @return The lane */ -static inline __attribute__((leaf, nonnull, nothrow, pure, warn_unused_result)) +static inline __attribute__((nonnull, nothrow, pure, warn_unused_result)) int_fast64_t libkeccak_to_lane(const char* restrict message, size_t msglen, long rr, long ww, size_t off) { long n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off); int_fast64_t rc = 0; - message += off + message += off; while (ww--) { rc <<= 8; @@ -228,7 +228,7 @@ int_fast64_t libkeccak_to_lane(const char* restrict message, size_t msglen, long * @param off The offset in the message * @return The lane */ -static inline __attribute__((leaf, nonnull, nothrow, pure, hot, warn_unused_result)) +static inline __attribute__((nonnull, nothrow, pure, hot, warn_unused_result)) int_fast64_t libkeccak_to_lane64(const char* restrict message, size_t msglen, long rr, size_t off) { long n = (long)((msglen < (size_t)rr ? msglen : (size_t)rr) - off); @@ -249,11 +249,11 @@ int_fast64_t libkeccak_to_lane64(const char* restrict message, size_t msglen, lo * `state->M` should have `state->r / 8` bytes left over at the end * @param bits The number of bits in the end of the message that does not make a whole byte */ -static __attribute__((leaf, nonnull, nothrow)) +static __attribute__((nonnull, nothrow)) void libkeccak_pad10star1(libkeccak_state_t* restrict state, long bits) { long i, r = state->r; - long nrf = len - !!bits; + long nrf = state->mptr - !!bits; long len = (nrf << 3) | bits; long ll = len % r; char b = bits ? (state->M[nrf] | (1 << bits)) : 1; @@ -285,7 +285,8 @@ void libkeccak_pad10star1(libkeccak_state_t* restrict state, long bits) static __attribute__((nonnull, nothrow)) void libkeccak_absorption_phase(libkeccak_state_t* restrict state, size_t len) { - long i = len / rr, w = state->w, rr = state->r >> 3, ww = state->w >> 3; + long w = state->w, rr = state->r >> 3, ww = state->w >> 3; + long i = len / rr; const char* restrict message = state->M; if (__builtin_expect(ww >= 8, 1)) /* ww > 8 is impossible, it is just for optimisation possibilities. */ while (i--) @@ -369,7 +370,7 @@ int libkeccak_update(libkeccak_state_t* restrict state, const char* restrict msg state->mptr -= len; libkeccak_absorption_phase(state, len); - __builtin_memmove(state->M, state->M + len, state->mptr * state->sizeof(char)); + __builtin_memmove(state->M, state->M + len, state->mptr * sizeof(char)); return 0; } @@ -379,21 +380,21 @@ int libkeccak_update(libkeccak_state_t* restrict state, const char* restrict msg * Absorb the last part of the message and squeeze the Keccak sponge * * @param state The hashing state - * @param msg The rest of the message, may be `NULL` + * @param msg The rest of the message, may be `NULL`, may be modified * @param msglen The length of the partial message * @param bits The number of bits at the end of the message not covered by `msglen` * @param suffix The suffix concatenate to the message, only '1':s and '0':s, and NUL-termination * @param hashsum Output paramter for the hashsum, may be `NULL` * @return Zero on success, -1 on error */ -int libkeccak_digest(libkeccak_state_t* restrict state, const char* restrict msg, size_t msglen, +int libkeccak_digest(libkeccak_state_t* restrict state, char* restrict msg, size_t msglen, size_t bits, const char* restrict suffix, char* restrict hashsum) { long len, ni, i, j = 0, k, ptr = 0, ext; long rr = state->r >> 3; long ww = state->w >> 3; long nn = (state->n + 7) >> 3; - long suffix_len = suffix ? strlen(suffix) : 0; + long suffix_len = suffix ? __builtin_strlen(suffix) : 0; const char* restrict message = msg; char* restrict new; @@ -435,7 +436,7 @@ int libkeccak_digest(libkeccak_state_t* restrict state, const char* restrict msg if (msglen) __builtin_memcpy(state->M + state->mptr, message, msglen * sizeof(char)); state->mptr += msglen; - libkeccak_pad10star1(state->M, state->mptr, state->r, bits); + libkeccak_pad10star1(state, bits); libkeccak_absorption_phase(state, state->mptr); if (hashsum != NULL) @@ -444,7 +445,7 @@ int libkeccak_digest(libkeccak_state_t* restrict state, const char* restrict msg for (i = (state->n - 1) / state->r; i--;) libkeccak_f(state); - return 0 + return 0; } diff --git a/src/libkeccak/digest.h b/src/libkeccak/digest.h index bbb4758..0283220 100644 --- a/src/libkeccak/digest.h +++ b/src/libkeccak/digest.h @@ -39,7 +39,7 @@ int libkeccak_update(libkeccak_state_t* restrict state, const char* restrict msg * Absorb the last part of the message and squeeze the Keccak sponge * * @param state The hashing state - * @param msg The rest of the message, may be `NULL` + * @param msg The rest of the message, may be `NULL`, may be modified * @param msglen The length of the partial message * @param bits The number of bits at the end of the message not covered by `msglen` * @param suffix The suffix concatenate to the message, only '1':s and '0':s, and NUL-termination @@ -47,7 +47,7 @@ int libkeccak_update(libkeccak_state_t* restrict state, const char* restrict msg * @return Zero on success, -1 on error */ __attribute__((nonnull(1))) -int libkeccak_digest(libkeccak_state_t* restrict state, const char* restrict msg, size_t msglen, +int libkeccak_digest(libkeccak_state_t* restrict state, char* restrict msg, size_t msglen, size_t bits, const char* restrict suffix, char* restrict hashsum); @@ -81,5 +81,5 @@ __attribute__((nonnull, nothrow)) void libkeccak_squeeze(libkeccak_state_t* restrict state, char* restrict hashsum); -#undef +#endif diff --git a/src/libkeccak/files.h b/src/libkeccak/files.h index 8283c0e..534019f 100644 --- a/src/libkeccak/files.h +++ b/src/libkeccak/files.h @@ -54,7 +54,7 @@ static inline __attribute__((nonnull(2, 3), artificial, gnu_inline)) int libkeccak_keccaksum_fd(int fd, libkeccak_state_t* restrict state, const libkeccak_spec_t* restrict spec, char* restrict hashsum) { - return libkeccak_generalised_sum_fd(fd, spec, NULL, hashsum); + return libkeccak_generalised_sum_fd(fd, state, spec, NULL, hashsum); } @@ -74,7 +74,7 @@ int libkeccak_sha3sum_fd(int fd, libkeccak_state_t* restrict state, { libkeccak_spec_t spec; libkeccak_spec_sha3(&spec, output); - return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_SHA3_SUFFIX, hashsum); + return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_SHA3_SUFFIX, hashsum); } @@ -95,7 +95,7 @@ int libkeccak_rawshakesum_fd(int fd, libkeccak_state_t* restrict state, { libkeccak_spec_t spec; libkeccak_spec_rawshake(&spec, semicapacity, output); - return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_RAWSHAKE_SUFFIX, hashsum); + return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_RAWSHAKE_SUFFIX, hashsum); } @@ -116,9 +116,9 @@ int libkeccak_shakesum_fd(int fd, libkeccak_state_t* restrict state, { libkeccak_spec_t spec; libkeccak_spec_shake(&spec, semicapacity, output); - return libkeccak_generalised_sum_fd(fd, &spec, LIBKECCAK_SHAKE_SUFFIX, hashsum); + return libkeccak_generalised_sum_fd(fd, state, &spec, LIBKECCAK_SHAKE_SUFFIX, hashsum); } -#undef +#endif diff --git a/src/libkeccak/generalised-spec.c b/src/libkeccak/generalised-spec.c index f793c71..d3e0b0f 100644 --- a/src/libkeccak/generalised-spec.c +++ b/src/libkeccak/generalised-spec.c @@ -30,12 +30,12 @@ * If you are interrested in finding errors, you should call * `libkeccak_spec_check(output)` if this function returns zero * - * @param spec The generalised input specifications, may be modified - * @param output The specification datastructure to fill in - * @return Zero if `spec` is valid, a `LIBKECCAK_GENERALISED_SPEC_ERROR_*` if an error was found + * @param spec The generalised input specifications, will be update with resolved automatic values + * @param output_spec The specification datastructure to fill in + * @return Zero if `spec` is valid, a `LIBKECCAK_GENERALISED_SPEC_ERROR_*` if an error was found */ int libkeccak_degeneralise_spec(libkeccak_generalised_spec_t* restrict spec, - libkeccak_spec_t* restrict output) + libkeccak_spec_t* restrict output_spec) { long state_size, word_size, capacity, bitrate, output; @@ -79,7 +79,7 @@ int libkeccak_degeneralise_spec(libkeccak_generalised_spec_t* restrict spec, } - if (!have(bitrate) && !have(capacity) && !have(opacity)) /* state_size? */ + if (!have(bitrate) && !have(capacity) && !have(output)) /* state_size? */ { state_size = deft(state_size, 1600L); output = ((state_size << 5) / 100L + 7L) & ~0x07L; @@ -110,6 +110,13 @@ int libkeccak_degeneralise_spec(libkeccak_generalised_spec_t* restrict spec, state_size = deft(state_size, bitrate + capacity); output = deft(output, capacity == 8 ? 8 : (capacity << 1)); } + + spec->capacity = output_spec->capacity = capacity; + spec->bitrate = output_spec->bitrate = bitrate; + spec->output = output_spec->output = output; + spec->state_size = state_size; + spec->word_size = state_size / 25; + return 0; } diff --git a/src/libkeccak/generalised-spec.h b/src/libkeccak/generalised-spec.h index d460e0d..dabfeca 100644 --- a/src/libkeccak/generalised-spec.h +++ b/src/libkeccak/generalised-spec.h @@ -131,7 +131,7 @@ typedef struct libkeccak_generalised_spec * * @param spec The specification datastructure to fill in */ -static inline __attribute__((leaf, nonnull, nothrow, unused)) +static inline __attribute__((nonnull, nothrow, unused)) void libkeccak_generalised_spec_initialise(libkeccak_generalised_spec_t* restrict spec) { spec->bitrate = LIBKECCAK_GENERALISED_SPEC_AUTOMATIC; @@ -145,14 +145,14 @@ void libkeccak_generalised_spec_initialise(libkeccak_generalised_spec_t* restric /** * Convert a `libkeccak_generalised_spec_t` to a `libkeccak_spec_t` * - * @param spec The generalised input specifications, may be modified - * @param output The specification datastructure to fill in - * @return Zero if `spec` is valid, a `LIBKECCAK_GENERALISED_SPEC_ERROR_*` if an error was found + * @param spec The generalised input specifications, will be update with resolved automatic values + * @param output_spec The specification datastructure to fill in + * @return Zero if `spec` is valid, a `LIBKECCAK_GENERALISED_SPEC_ERROR_*` if an error was found */ __attribute__((leaf, nonnull, nothrow)) int libkeccak_degeneralise_spec(libkeccak_generalised_spec_t* restrict spec, - libkeccak_spec_t* restrict output); + libkeccak_spec_t* restrict output_spec); -#undef +#endif diff --git a/src/libkeccak/hex.h b/src/libkeccak/hex.h index 5d9470c..ae32669 100644 --- a/src/libkeccak/hex.h +++ b/src/libkeccak/hex.h @@ -56,5 +56,5 @@ __attribute__((leaf, nonnull, nothrow)) void libkeccak_unhex(char* restrict output, const char* restrict hashsum); -#undef +#endif diff --git a/src/libkeccak/spec.h b/src/libkeccak/spec.h index d9608a6..5c36b6a 100644 --- a/src/libkeccak/spec.h +++ b/src/libkeccak/spec.h @@ -106,7 +106,7 @@ typedef struct libkeccak_spec * @param spec The specifications datastructure to fill in * @param x The value of x in `SHA3-x`, the output size */ -static inline __attribute__((leaf, nonnull, nothrow)) +static inline __attribute__((nonnull, nothrow)) void libkeccak_spec_sha3(libkeccak_spec_t* restrict spec, long x) { spec->bitrate = 1600 - 2 * x; @@ -122,7 +122,7 @@ void libkeccak_spec_sha3(libkeccak_spec_t* restrict spec, long x) * @param x The value of x in `RawSHAKEx`, half the capacity * @param d The output size */ -static inline __attribute__((leaf, nonnull, nothrow)) +static inline __attribute__((nonnull, nothrow)) void libkeccak_spec_rawshake(libkeccak_spec_t* restrict spec, long x, long d) { spec->bitrate = 1600 - 2 * x; @@ -147,7 +147,7 @@ void libkeccak_spec_rawshake(libkeccak_spec_t* restrict spec, long x, long d) * @param spec The specifications datastructure to check * @return Zero if error free, a `LIBKECCAK_SPEC_ERROR_*` if an error was found */ -static inline __attribute__((leaf, nonnull, nothrow, unused, warn_unused_result, pure)) +static inline __attribute__((nonnull, nothrow, unused, warn_unused_result, pure)) int libkeccak_spec_check(const libkeccak_spec_t* restrict spec) { long state_size = spec->capacity + spec->bitrate; @@ -162,5 +162,5 @@ int libkeccak_spec_check(const libkeccak_spec_t* restrict spec) } -#undef +#endif diff --git a/src/libkeccak/state.c b/src/libkeccak/state.c index 31b5b7b..532a33c 100644 --- a/src/libkeccak/state.c +++ b/src/libkeccak/state.c @@ -35,18 +35,18 @@ int libkeccak_state_initialise(libkeccak_state_t* restrict state, const libkecca state->r = spec->bitrate; state->n = spec->output; state->c = spec->capacity; - state->b = r + c; - state->w = x = b / 25; + 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 + (l << 1); - state->wmod = (state->w == 64) ? ~0LL : ((1LL << w) - 1); + state->nr = 12 + (state->l << 1); + state->wmod = (state->w == 64) ? ~0LL : ((1LL << state->w) - 1); for (x = 0; x < 25; x++) state->S[x] = 0; state->mptr = 0; - state->mlen = (r * b) >> 2; + state->mlen = (state->r * state->b) >> 2; state->M = malloc(state->mlen * sizeof(char)); return state->M == NULL ? -1 : 0; } @@ -84,7 +84,7 @@ int libkeccak_state_copy(libkeccak_state_t* restrict dest, const libkeccak_state memcpy(dest, src, sizeof(libkeccak_state_t)); dest->M = malloc(src->mlen * sizeof(char)); if (dest->M == NULL) - return dest->M = NULL, NULL; + return -1; memcpy(dest->M, src->M, src->mptr * sizeof(char)); return 0; } diff --git a/src/libkeccak/state.h b/src/libkeccak/state.h index 0819b6a..7ee0152 100644 --- a/src/libkeccak/state.h +++ b/src/libkeccak/state.h @@ -116,7 +116,7 @@ int libkeccak_state_initialise(libkeccak_state_t* restrict state, const libkecca * * @param state The state that should be destroyed */ -static inline __attribute__((leaf)) +static inline void libkeccak_state_fast_destroy(libkeccak_state_t* restrict state) { if (state == NULL) @@ -163,7 +163,7 @@ libkeccak_state_t* libkeccak_state_create(const libkeccak_spec_t* restrict spec) { libkeccak_state_t* restrict state = malloc(sizeof(libkeccak_state_t)); int saved_errno; - if ((state == NULL) || libkeccak_state_initialise(state)) + if ((state == NULL) || libkeccak_state_initialise(state, spec)) return saved_errno = errno, free(state), errno = saved_errno, NULL; return state; } @@ -191,7 +191,7 @@ static inline __attribute__((unused, optimize("-O0"))) void libkeccak_state_free(volatile libkeccak_state_t* restrict state) { libkeccak_state_destroy(state); - free(state); + free((libkeccak_state_t*)state); } @@ -230,7 +230,7 @@ libkeccak_state_t* libkeccak_state_duplicate(const libkeccak_state_t* restrict s * @param state The state as it will be marshalled by a subsequent call to `libkeccak_state_marshal` * @return The allocation size needed for the buffer to which the state will be marshalled */ -static inline __attribute__((leaf, nonnull, nothrow, unused, warn_unused_result, pure)) +static inline __attribute__((nonnull, nothrow, unused, warn_unused_result, pure)) size_t libkeccak_state_marshal_size(const libkeccak_state_t* restrict state) { return sizeof(libkeccak_state_t) - sizeof(char*) + state->mptr * sizeof(char); @@ -270,5 +270,5 @@ __attribute__((leaf, nonnull, nothrow, warn_unused_result, pure)) size_t libkeccak_state_unmarshal_skip(const char* restrict data); -#undef +#endif -- cgit v1.2.3-70-g09d2