siphash: Use more hygienic state initialiser
The PREAMBLE macro sets up the SipHash initial internal state. It also defines that state as a variable, which isn't macro hygeinic. With previous changes simplifying this premable, it's now possible to replace it with a macro which simply expands to a C initialisedrfor that state. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
5cc843521d
commit
fcec3f6f9d
1 changed files with 12 additions and 17 deletions
29
siphash.c
29
siphash.c
|
@ -58,15 +58,12 @@
|
||||||
|
|
||||||
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
|
#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
|
||||||
|
|
||||||
#define PREAMBLE \
|
#define SIPHASH_INIT(k) { \
|
||||||
uint64_t v[4] = { 0x736f6d6570736575ULL, 0x646f72616e646f6dULL, \
|
0x736f6d6570736575ULL ^ (k)[0], \
|
||||||
0x6c7967656e657261ULL, 0x7465646279746573ULL }; \
|
0x646f72616e646f6dULL ^ (k)[1], \
|
||||||
int __i; \
|
0x6c7967656e657261ULL ^ (k)[0], \
|
||||||
\
|
0x7465646279746573ULL ^ (k)[1] \
|
||||||
do { \
|
}
|
||||||
for (__i = sizeof(v) / sizeof(v[0]) - 1; __i >= 0; __i--) \
|
|
||||||
v[__i] ^= k[__i % 2]; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sipround() - Perform rounds of SipHash scrambling
|
* sipround() - Perform rounds of SipHash scrambling
|
||||||
|
@ -140,7 +137,8 @@ __attribute__((optimize("-fno-strict-aliasing")))
|
||||||
/* cppcheck-suppress unusedFunction */
|
/* cppcheck-suppress unusedFunction */
|
||||||
uint64_t siphash_8b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_8b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
PREAMBLE;
|
uint64_t v[4] = SIPHASH_INIT(k);
|
||||||
|
|
||||||
siphash_feed(v, *(uint64_t *)in);
|
siphash_feed(v, *(uint64_t *)in);
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,8 +158,8 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */
|
||||||
uint64_t siphash_12b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_12b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
uint32_t *in32 = (uint32_t *)in;
|
uint32_t *in32 = (uint32_t *)in;
|
||||||
|
uint64_t v[4] = SIPHASH_INIT(k);
|
||||||
|
|
||||||
PREAMBLE;
|
|
||||||
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
||||||
|
|
||||||
return siphash_final(v, 12, *(in32 + 2));
|
return siphash_final(v, 12, *(in32 + 2));
|
||||||
|
@ -179,10 +177,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */
|
||||||
uint64_t siphash_20b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_20b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
uint32_t *in32 = (uint32_t *)in;
|
uint32_t *in32 = (uint32_t *)in;
|
||||||
|
uint64_t v[4] = SIPHASH_INIT(k);
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
PREAMBLE;
|
|
||||||
|
|
||||||
for (i = 0; i < 2; i++, in32 += 2)
|
for (i = 0; i < 2; i++, in32 += 2)
|
||||||
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
||||||
|
|
||||||
|
@ -202,10 +199,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */
|
||||||
uint64_t siphash_32b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_32b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
uint64_t *in64 = (uint64_t *)in;
|
uint64_t *in64 = (uint64_t *)in;
|
||||||
|
uint64_t v[4] = SIPHASH_INIT(k);
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
PREAMBLE;
|
|
||||||
|
|
||||||
for (i = 0; i < 4; i++, in64++)
|
for (i = 0; i < 4; i++, in64++)
|
||||||
siphash_feed(v, *in64);
|
siphash_feed(v, *in64);
|
||||||
|
|
||||||
|
@ -224,10 +220,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */
|
||||||
uint64_t siphash_36b(const uint8_t *in, const uint64_t *k)
|
uint64_t siphash_36b(const uint8_t *in, const uint64_t *k)
|
||||||
{
|
{
|
||||||
uint32_t *in32 = (uint32_t *)in;
|
uint32_t *in32 = (uint32_t *)in;
|
||||||
|
uint64_t v[4] = SIPHASH_INIT(k);
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
PREAMBLE;
|
|
||||||
|
|
||||||
for (i = 0; i < 4; i++, in32 += 2)
|
for (i = 0; i < 4; i++, in32 += 2)
|
||||||
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue