diff options
author | Mattias Andrée <maandree@kth.se> | 2024-09-15 00:37:13 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-09-15 00:37:13 +0200 |
commit | 0ab7b5a1ce4ed6735b40e167f8b05b8bf1fede59 (patch) | |
tree | af2a138767835010d50611790265bbd2bdc2adae /libkeccak_state_marshal.c | |
parent | m (diff) | |
download | libkeccak-0ab7b5a1ce4ed6735b40e167f8b05b8bf1fede59.tar.gz libkeccak-0ab7b5a1ce4ed6735b40e167f8b05b8bf1fede59.tar.bz2 libkeccak-0ab7b5a1ce4ed6735b40e167f8b05b8bf1fede59.tar.xz |
Split libkeccak.h and fix support for architectures that do not allow misaligned memory
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libkeccak_state_marshal.c')
-rw-r--r-- | libkeccak_state_marshal.c | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/libkeccak_state_marshal.c b/libkeccak_state_marshal.c index 6563038..480b4a4 100644 --- a/libkeccak_state_marshal.c +++ b/libkeccak_state_marshal.c @@ -17,9 +17,15 @@ 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) +#define set(VAR)\ + do {\ + __builtin_memcpy(data, &state->VAR, sizeof(state->VAR));\ + data += sizeof(state->VAR);\ + } while (0) + unsigned char *restrict start = data_; unsigned char *restrict data = start; + if (!data) { return 7 * sizeof(long int) + 1 * sizeof(int64_t) + @@ -27,20 +33,23 @@ libkeccak_state_marshal(const struct libkeccak_state *restrict state, void *rest 2 * sizeof(size_t) + state->mptr; } - set(long int, r); - set(long int, c); - set(long int, n); - set(long int, b); - set(long int, w); - set(int64_t, wmod); - set(long int, l); - set(long int, nr); - memcpy(data, state->S, sizeof(state->S)); + + set(r); + set(c); + set(n); + set(b); + set(w); + set(wmod); + set(l); + set(nr); + __builtin_memcpy(data, state->S, sizeof(state->S)); data += sizeof(state->S); - set(size_t, mptr); - set(size_t, mlen); + set(mptr); + set(mlen); memcpy(data, state->M, state->mptr * sizeof(char)); data += state->mptr; + return (size_t)(data - start); + #undef set } |