From 0ab7b5a1ce4ed6735b40e167f8b05b8bf1fede59 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 15 Sep 2024 00:37:13 +0200 Subject: Split libkeccak.h and fix support for architectures that do not allow misaligned memory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- libkeccak_state_unmarshal.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'libkeccak_state_unmarshal.c') diff --git a/libkeccak_state_unmarshal.c b/libkeccak_state_unmarshal.c index f221fad..f9604a2 100644 --- a/libkeccak_state_unmarshal.c +++ b/libkeccak_state_unmarshal.c @@ -18,31 +18,38 @@ 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) +#define get(VAR) \ + do {\ + __builtin_memcpy(&state->VAR, data, sizeof(state->VAR));\ + data += sizeof(state->VAR);\ + } while (0) + const unsigned char *restrict start = data_; const unsigned char *restrict data = start; size_t mptr; + if (!state) { - data += 7 * sizeof(long int); - data += 1 * sizeof(int64_t); + data += 7U * sizeof(long int); + data += 1U * sizeof(int64_t); data += sizeof(state->S); mptr = *(const size_t *)data; - data += 2 * sizeof(size_t); + data += 2U * sizeof(size_t); data += mptr; return (size_t)(data - start); } - get(long int, r); - get(long int, c); - get(long int, n); - get(long int, b); - get(long int, w); - get(int64_t, wmod); - get(long int, l); - get(long int, nr); + + get(r); + get(c); + get(n); + get(b); + get(w); + get(wmod); + get(l); + get(nr); memcpy(state->S, data, sizeof(state->S)); data += sizeof(state->S); - get(size_t, mptr); - get(size_t, mlen); + get(mptr); + get(mlen); if (state->mptr) { state->M = malloc(state->mptr * sizeof(char)); if (!state->M) @@ -52,6 +59,8 @@ libkeccak_state_unmarshal(struct libkeccak_state *restrict state, const void *re } else { state->M = NULL; } + return (size_t)(data - start); + #undef get } -- cgit v1.2.3-70-g09d2