2019-09-10 11:19:01 +02:00
|
|
|
// SPDX-License-Identifier: LGPL-2.1+
|
2019-09-25 13:13:40 +02:00
|
|
|
/*
|
2019-10-01 09:20:35 +02:00
|
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
2017-10-14 13:28:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "nm-default.h"
|
|
|
|
|
|
|
|
|
|
#include "nm-hash-utils.h"
|
|
|
|
|
|
2017-10-13 17:16:23 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2017-10-14 13:28:20 +02:00
|
|
|
#include "nm-shared-utils.h"
|
|
|
|
|
#include "nm-random-utils.h"
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2017-10-13 17:16:23 +02:00
|
|
|
#define HASH_KEY_SIZE 16u
|
|
|
|
|
#define HASH_KEY_SIZE_GUINT ((HASH_KEY_SIZE + sizeof (guint) - 1) / sizeof (guint))
|
|
|
|
|
|
|
|
|
|
G_STATIC_ASSERT (sizeof (guint) * HASH_KEY_SIZE_GUINT >= HASH_KEY_SIZE);
|
|
|
|
|
|
2017-11-16 11:40:19 +01:00
|
|
|
static const guint8 *volatile global_seed = NULL;
|
|
|
|
|
|
2017-10-13 17:16:23 +02:00
|
|
|
static const guint8 *
|
2017-11-16 11:40:19 +01:00
|
|
|
_get_hash_key_init (void)
|
2017-10-13 17:16:23 +02:00
|
|
|
{
|
2017-11-15 15:39:50 +01:00
|
|
|
/* the returned hash is aligned to guin64, hence, it is safe
|
|
|
|
|
* to use it as guint* or guint64* pointer. */
|
|
|
|
|
static union {
|
|
|
|
|
guint8 v8[HASH_KEY_SIZE];
|
2019-05-10 08:03:54 +02:00
|
|
|
guint _align_as_uint;
|
|
|
|
|
guint32 _align_as_uint32;
|
|
|
|
|
guint64 _align_as_uint64;
|
|
|
|
|
} g_arr;
|
2017-10-13 17:16:23 +02:00
|
|
|
const guint8 *g;
|
|
|
|
|
|
2018-11-13 09:25:55 +01:00
|
|
|
again:
|
|
|
|
|
g = g_atomic_pointer_get (&global_seed);
|
2020-03-24 11:31:27 +01:00
|
|
|
if (!G_UNLIKELY (g)) {
|
|
|
|
|
static gsize g_lock;
|
2018-11-13 09:25:55 +01:00
|
|
|
uint64_t h;
|
2020-03-24 11:31:27 +01:00
|
|
|
union {
|
|
|
|
|
guint vuint;
|
|
|
|
|
guint8 v8[HASH_KEY_SIZE];
|
|
|
|
|
guint8 _extra_entropy[3 * HASH_KEY_SIZE];
|
|
|
|
|
} t_arr;
|
2018-11-13 09:25:55 +01:00
|
|
|
|
|
|
|
|
nm_utils_random_bytes (&t_arr, sizeof (t_arr));
|
2017-11-15 15:39:50 +01:00
|
|
|
|
2020-03-24 11:31:27 +01:00
|
|
|
/* We only initialize one random hash key. So we can spend some effort
|
|
|
|
|
* of getting this right. For one, we collect more random bytes than
|
|
|
|
|
* necessary.
|
2018-11-13 09:25:55 +01:00
|
|
|
*
|
2020-03-24 11:31:27 +01:00
|
|
|
* Then, the first guint of the seed should have all the entropy that we could
|
|
|
|
|
* obtain in sizeof(t_arr). For that, siphash(t_arr) and xor the first guint
|
|
|
|
|
* with hash.
|
|
|
|
|
* The first guint is especially interesting for nm_hash_static() below that
|
|
|
|
|
* doesn't use siphash itself. */
|
|
|
|
|
h = c_siphash_hash (t_arr.v8,
|
|
|
|
|
(const guint8 *) &t_arr,
|
|
|
|
|
sizeof (t_arr));
|
|
|
|
|
if (sizeof (h) > sizeof (guint))
|
|
|
|
|
t_arr.vuint = t_arr.vuint ^ ((guint) (h & G_MAXUINT)) ^ ((guint) (h >> 32));
|
2017-11-15 15:39:50 +01:00
|
|
|
else
|
2020-03-24 11:31:27 +01:00
|
|
|
t_arr.vuint = t_arr.vuint ^ ((guint) (h & G_MAXUINT));
|
|
|
|
|
|
|
|
|
|
if (!g_once_init_enter (&g_lock)) {
|
|
|
|
|
/* lost a race. The random key is already initialized. */
|
|
|
|
|
goto again;
|
|
|
|
|
}
|
2017-11-15 15:39:50 +01:00
|
|
|
|
2020-03-24 11:31:27 +01:00
|
|
|
memcpy (g_arr.v8, t_arr.v8, HASH_KEY_SIZE);
|
|
|
|
|
g = g_arr.v8;
|
|
|
|
|
g_atomic_pointer_set (&global_seed, g);
|
|
|
|
|
g_once_init_leave (&g_lock, 1);
|
2017-11-15 15:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 11:31:27 +01:00
|
|
|
nm_assert (g == g_arr.v8);
|
2018-11-13 09:25:55 +01:00
|
|
|
return g;
|
2017-11-15 15:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
2017-11-16 11:40:19 +01:00
|
|
|
#define _get_hash_key() \
|
|
|
|
|
({ \
|
|
|
|
|
const guint8 *_g; \
|
|
|
|
|
\
|
2018-11-13 09:25:55 +01:00
|
|
|
_g = g_atomic_pointer_get (&global_seed); \
|
|
|
|
|
if (G_UNLIKELY (!_g)) \
|
2017-11-16 11:40:19 +01:00
|
|
|
_g = _get_hash_key_init (); \
|
|
|
|
|
_g; \
|
|
|
|
|
})
|
|
|
|
|
|
2017-11-15 15:39:50 +01:00
|
|
|
guint
|
|
|
|
|
nm_hash_static (guint static_seed)
|
|
|
|
|
{
|
2020-03-24 11:31:27 +01:00
|
|
|
/* Note that we only xor the static_seed with the first guint of the key.
|
|
|
|
|
*
|
|
|
|
|
* We don't use siphash, which would mix the bits better with _get_hash_key().
|
|
|
|
|
* Note that nm_hash_static() isn't used to hash the static_seed. Instead, it
|
|
|
|
|
* is used to get a unique hash value in a static context. That means, every
|
|
|
|
|
* caller is responsible to choose a static_seed that is sufficiently
|
|
|
|
|
* distinct from all other callers. In other words, static_seed should be a
|
|
|
|
|
* unique constant with good entropy.
|
|
|
|
|
*
|
|
|
|
|
* Note that _get_hash_key_init() already xored the first guint of the
|
|
|
|
|
* key with the siphash of the entire static key. That means, even if
|
|
|
|
|
* we got bad randomness for the first guint, the first guint is also
|
|
|
|
|
* mixed with the randomness of the entire random key.
|
2017-11-15 15:39:50 +01:00
|
|
|
*
|
2020-03-24 11:31:27 +01:00
|
|
|
* Also, ensure that we don't return zero (like for nm_hash_complete()).
|
2017-11-15 15:39:50 +01:00
|
|
|
*/
|
2020-03-24 11:31:27 +01:00
|
|
|
return ((*((const guint *) _get_hash_key ())) ^ static_seed)
|
|
|
|
|
?: 3679500967u;
|
2017-10-13 17:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 14:00:22 +02:00
|
|
|
void
|
2018-12-12 12:11:53 +01:00
|
|
|
nm_hash_siphash42_init (CSipHash *h, guint static_seed)
|
2017-10-14 13:28:20 +02:00
|
|
|
{
|
2017-10-13 17:16:23 +02:00
|
|
|
const guint8 *g;
|
2019-05-10 08:03:54 +02:00
|
|
|
union {
|
|
|
|
|
guint64 _align_as_uint64;
|
|
|
|
|
guint arr[HASH_KEY_SIZE_GUINT];
|
|
|
|
|
} seed;
|
2017-10-14 13:28:20 +02:00
|
|
|
|
2018-12-12 12:11:53 +01:00
|
|
|
nm_assert (h);
|
2017-10-13 14:00:22 +02:00
|
|
|
|
2017-10-13 17:16:23 +02:00
|
|
|
g = _get_hash_key ();
|
2019-05-10 08:03:54 +02:00
|
|
|
memcpy (&seed, g, HASH_KEY_SIZE);
|
|
|
|
|
seed.arr[0] ^= static_seed;
|
|
|
|
|
c_siphash_init (h, (const guint8 *) &seed);
|
2017-10-13 14:00:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guint
|
|
|
|
|
nm_hash_str (const char *str)
|
|
|
|
|
{
|
|
|
|
|
NMHashState h;
|
|
|
|
|
|
2017-11-15 15:14:19 +01:00
|
|
|
if (!str)
|
|
|
|
|
return nm_hash_static (1867854211u);
|
|
|
|
|
nm_hash_init (&h, 1867854211u);
|
|
|
|
|
nm_hash_update_str (&h, str);
|
2017-10-13 14:00:22 +02:00
|
|
|
return nm_hash_complete (&h);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guint
|
|
|
|
|
nm_str_hash (gconstpointer str)
|
|
|
|
|
{
|
|
|
|
|
return nm_hash_str (str);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 17:16:23 +02:00
|
|
|
guint
|
|
|
|
|
nm_hash_ptr (gconstpointer ptr)
|
|
|
|
|
{
|
2017-11-15 15:33:32 +01:00
|
|
|
NMHashState h;
|
2017-10-13 17:16:23 +02:00
|
|
|
|
2017-11-15 15:33:32 +01:00
|
|
|
if (!ptr)
|
|
|
|
|
return nm_hash_static (2907677551u);
|
|
|
|
|
nm_hash_init (&h, 2907677551u);
|
|
|
|
|
nm_hash_update (&h, &ptr, sizeof (ptr));
|
|
|
|
|
return nm_hash_complete (&h);
|
2017-10-13 17:16:23 +02:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 14:00:22 +02:00
|
|
|
guint
|
|
|
|
|
nm_direct_hash (gconstpointer ptr)
|
|
|
|
|
{
|
|
|
|
|
return nm_hash_ptr (ptr);
|
2017-10-14 13:28:20 +02:00
|
|
|
}
|
2018-04-05 10:21:56 +02:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
guint
|
|
|
|
|
nm_pstr_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
const char *const*s = p;
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return nm_hash_static (101061439u);
|
|
|
|
|
return nm_hash_str (*s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_pstr_equal (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
const char *const*s1 = a;
|
|
|
|
|
const char *const*s2 = b;
|
|
|
|
|
|
|
|
|
|
return (s1 == s2)
|
|
|
|
|
|| ( s1
|
|
|
|
|
&& s2
|
|
|
|
|
&& nm_streq0 (*s1, *s2));
|
|
|
|
|
}
|
2019-07-02 16:13:38 +02:00
|
|
|
|
2019-11-07 15:17:39 +01:00
|
|
|
guint
|
|
|
|
|
nm_pint_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
const int *s = p;
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return nm_hash_static (298377461u);
|
|
|
|
|
return nm_hash_val (1208815757u, *s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_pint_equals (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
const int *s1 = a;
|
|
|
|
|
const int *s2 = a;
|
|
|
|
|
|
|
|
|
|
return s1 == s2
|
|
|
|
|
|| (s1 && s2 && *s1 == *s2);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 16:13:38 +02:00
|
|
|
guint
|
|
|
|
|
nm_pdirect_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
const void *const*s = p;
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return nm_hash_static (1852748873u);
|
|
|
|
|
return nm_direct_hash (*s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_pdirect_equal (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
const void *const*s1 = a;
|
|
|
|
|
const void *const*s2 = b;
|
|
|
|
|
|
|
|
|
|
return (s1 == s2)
|
|
|
|
|
|| ( s1
|
|
|
|
|
&& s2
|
|
|
|
|
&& *s1 == *s2);
|
|
|
|
|
}
|
2019-10-27 09:23:39 +01:00
|
|
|
|
|
|
|
|
guint
|
|
|
|
|
nm_ppdirect_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
const void *const*const*s = p;
|
|
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
|
return nm_hash_static (396534869u);
|
|
|
|
|
if (!*s)
|
|
|
|
|
return nm_hash_static (1476102263u);
|
|
|
|
|
return nm_direct_hash (**s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_ppdirect_equal (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
const void *const*const*s1 = a;
|
|
|
|
|
const void *const*const*s2 = b;
|
|
|
|
|
|
|
|
|
|
if (s1 == s2)
|
|
|
|
|
return TRUE;
|
|
|
|
|
if (!s1 || !s2)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (*s1 == *s2)
|
|
|
|
|
return TRUE;
|
|
|
|
|
if (!*s1 || !*s2)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return **s1 == **s2;
|
|
|
|
|
}
|
2020-03-20 22:47:40 +01:00
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
2020-04-27 08:02:11 +02:00
|
|
|
guint
|
|
|
|
|
nm_gbytes_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
GBytes *ptr = (GBytes *) p;
|
|
|
|
|
gconstpointer arr;
|
|
|
|
|
gsize len;
|
|
|
|
|
|
|
|
|
|
arr = g_bytes_get_data (ptr, &len);
|
|
|
|
|
return nm_hash_mem (792701303u, arr, len);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-20 22:47:40 +01:00
|
|
|
guint
|
|
|
|
|
nm_pgbytes_hash (gconstpointer p)
|
|
|
|
|
{
|
|
|
|
|
GBytes *const*ptr = p;
|
|
|
|
|
gconstpointer arr;
|
|
|
|
|
gsize len;
|
|
|
|
|
|
|
|
|
|
arr = g_bytes_get_data (*ptr, &len);
|
|
|
|
|
return nm_hash_mem (1470631313u, arr, len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_pgbytes_equal (gconstpointer a, gconstpointer b)
|
|
|
|
|
{
|
|
|
|
|
GBytes *const*ptr_a = a;
|
|
|
|
|
GBytes *const*ptr_b = b;
|
|
|
|
|
|
|
|
|
|
return g_bytes_equal (*ptr_a, *ptr_b);
|
|
|
|
|
}
|