diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-11-07 03:55:26 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-11-07 03:55:26 +0100 |
commit | ad37c140346ebf96f376d0f984b066d6604a8578 (patch) | |
tree | 1af35f66c2ceec1a04a42a0f11e7f0aa0db21d39 /src | |
parent | small optimisations to libkeccak_pad10star1 (diff) | |
download | libkeccak-ad37c140346ebf96f376d0f984b066d6604a8578.tar.gz libkeccak-ad37c140346ebf96f376d0f984b066d6604a8578.tar.bz2 libkeccak-ad37c140346ebf96f376d0f984b066d6604a8578.tar.xz |
add check for that the word size is 2-potent
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/libkeccak/spec.h | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/libkeccak/spec.h b/src/libkeccak/spec.h index c72c04d..bc5f6c3 100644 --- a/src/libkeccak/spec.h +++ b/src/libkeccak/spec.h @@ -20,6 +20,9 @@ #define LIBKECCAK_SPEC_H 1 +#include <limits.h> + + /** * Message suffix for SHA3 hashing */ @@ -62,7 +65,7 @@ #define LIBKECCAK_SPEC_ERROR_OUTPUT_NONPOSITIVE 5 /** - * Invalid `libkeccak_spec_t` values: `.bitrate + `.capacity` + * Invalid `libkeccak_spec_t` values: `.bitrate + `.capacity` * is greater 1600 which is the largest supported state size */ #define LIBKECCAK_SPEC_ERROR_STATE_TOO_LARGE 6 @@ -73,6 +76,12 @@ */ #define LIBKECCAK_SPEC_ERROR_STATE_MOD_25 7 +/** + * Invalid `libkeccak_spec_t` values: `.bitrate + `.capacity` + * is a not a 2-potent multiple of 25 + */ +#define LIBKECCAK_SPEC_ERROR_WORD_NON_2_POTENT 8 + /** @@ -150,7 +159,7 @@ void libkeccak_spec_rawshake(libkeccak_spec_t* restrict spec, long x, long d) 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; + long state_size = spec->capacity + spec->bitrate, n_state_size; if (spec->bitrate <= 0) return LIBKECCAK_SPEC_ERROR_BITRATE_NONPOSITIVE; if (spec->bitrate % 8) return LIBKECCAK_SPEC_ERROR_BITRATE_MOD_8; if (spec->capacity <= 0) return LIBKECCAK_SPEC_ERROR_CAPACITY_NONPOSITIVE; @@ -158,6 +167,14 @@ int libkeccak_spec_check(const libkeccak_spec_t* restrict spec) if (spec->output <= 0) return LIBKECCAK_SPEC_ERROR_OUTPUT_NONPOSITIVE; if (state_size > 1600) return LIBKECCAK_SPEC_ERROR_STATE_TOO_LARGE; if (state_size % 25) return LIBKECCAK_SPEC_ERROR_STATE_MOD_25; + state_size /= 25; + + /* This is a portable implementation of `(x & -x) != x` which assumes + * two's complement, which of course is always satisfied by GCC, but anyway... */ + n_state_size = ((~state_size) ^ (LONG_MIN & ~LONG_MAX)) + 1; + if ((state_size & n_state_size) != state_size) + return LIBKECCAK_SPEC_ERROR_WORD_NON_2_POTENT; + return 0; } |