2003-07-24 22:22:26 -04:00
|
|
|
/*
|
2009-05-06 19:47:50 -04:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2003-07-24 22:22:26 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 02:37:05 -04:00
|
|
|
*
|
2003-07-24 22:22:26 -04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 03:53:12 -05:00
|
|
|
*
|
2003-07-24 22:22:26 -04:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-03-28 08:19:37 -04:00
|
|
|
#include <inttypes.h>
|
2018-04-17 11:29:14 -04:00
|
|
|
#include <stdbool.h>
|
2018-03-28 07:59:57 -04:00
|
|
|
#include <stddef.h>
|
2018-03-28 08:19:37 -04:00
|
|
|
|
2022-09-22 04:36:53 -04:00
|
|
|
#include <isc/ascii.h>
|
|
|
|
|
#include <isc/entropy.h>
|
|
|
|
|
#include <isc/hash.h> /* IWYU pragma: keep */
|
|
|
|
|
#include <isc/once.h>
|
|
|
|
|
#include <isc/random.h>
|
|
|
|
|
#include <isc/result.h>
|
|
|
|
|
#include <isc/siphash.h>
|
|
|
|
|
#include <isc/string.h>
|
|
|
|
|
#include <isc/types.h>
|
|
|
|
|
#include <isc/util.h>
|
2003-07-24 22:22:26 -04:00
|
|
|
|
2019-04-04 07:51:09 -04:00
|
|
|
static uint8_t isc_hash_key[16];
|
2020-07-16 11:29:44 -04:00
|
|
|
static uint8_t isc_hash32_key[8];
|
2019-04-04 07:51:09 -04:00
|
|
|
static bool hash_initialized = false;
|
|
|
|
|
static isc_once_t isc_hash_once = ISC_ONCE_INIT;
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
isc_hash_initialize(void) {
|
|
|
|
|
/*
|
|
|
|
|
* Set a constant key to help in problem reproduction should
|
|
|
|
|
* fuzzing find a crash or a hang.
|
|
|
|
|
*/
|
2020-07-16 11:29:44 -04:00
|
|
|
uint64_t key[2] = { 0, 1 };
|
|
|
|
|
#if !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2019-04-04 07:51:09 -04:00
|
|
|
isc_entropy_get(key, sizeof(key));
|
|
|
|
|
#endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
|
|
|
|
memmove(isc_hash_key, key, sizeof(isc_hash_key));
|
2020-07-16 11:29:44 -04:00
|
|
|
#if !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
|
|
|
isc_entropy_get(key, sizeof(key));
|
|
|
|
|
#endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
|
|
|
|
memmove(isc_hash32_key, key, sizeof(isc_hash32_key));
|
2019-04-04 07:51:09 -04:00
|
|
|
hash_initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-12 03:22:45 -05:00
|
|
|
const void *
|
|
|
|
|
isc_hash_get_initializer(void) {
|
2021-10-14 04:33:24 -04:00
|
|
|
if (!hash_initialized) {
|
2022-10-14 10:10:41 -04:00
|
|
|
isc_once_do(&isc_hash_once, isc_hash_initialize);
|
2019-04-04 07:51:09 -04:00
|
|
|
}
|
2016-02-12 03:22:45 -05:00
|
|
|
|
2019-04-04 07:51:09 -04:00
|
|
|
return (isc_hash_key);
|
2016-02-12 03:22:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
isc_hash_set_initializer(const void *initializer) {
|
|
|
|
|
REQUIRE(initializer != NULL);
|
|
|
|
|
|
|
|
|
|
/*
|
2019-04-04 07:51:09 -04:00
|
|
|
* Ensure that isc_hash_initialize() is not called after
|
2016-02-12 03:22:45 -05:00
|
|
|
* isc_hash_set_initializer() is called.
|
|
|
|
|
*/
|
2021-10-14 04:33:24 -04:00
|
|
|
if (!hash_initialized) {
|
2022-10-14 10:10:41 -04:00
|
|
|
isc_once_do(&isc_hash_once, isc_hash_initialize);
|
2019-04-04 07:51:09 -04:00
|
|
|
}
|
2016-02-12 03:22:45 -05:00
|
|
|
|
2019-04-04 07:51:09 -04:00
|
|
|
memmove(isc_hash_key, initializer, sizeof(isc_hash_key));
|
2016-02-12 03:22:45 -05:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 07:51:09 -04:00
|
|
|
uint64_t
|
2020-07-16 11:29:44 -04:00
|
|
|
isc_hash64(const void *data, const size_t length, const bool case_sensitive) {
|
2019-04-04 07:51:09 -04:00
|
|
|
uint64_t hval;
|
2015-12-09 08:37:20 -05:00
|
|
|
|
2016-08-29 09:22:49 -04:00
|
|
|
REQUIRE(length == 0 || data != NULL);
|
2017-04-21 22:55:10 -04:00
|
|
|
|
2022-10-14 10:10:41 -04:00
|
|
|
isc_once_do(&isc_hash_once, isc_hash_initialize);
|
2015-12-09 08:37:20 -05:00
|
|
|
|
2022-06-27 04:13:19 -04:00
|
|
|
isc_siphash24(isc_hash_key, data, length, case_sensitive,
|
|
|
|
|
(uint8_t *)&hval);
|
2015-12-09 08:37:20 -05:00
|
|
|
|
|
|
|
|
return (hval);
|
|
|
|
|
}
|
2020-07-16 11:29:44 -04:00
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
isc_hash32(const void *data, const size_t length, const bool case_sensitive) {
|
|
|
|
|
uint32_t hval;
|
|
|
|
|
|
|
|
|
|
REQUIRE(length == 0 || data != NULL);
|
|
|
|
|
|
2022-10-14 10:10:41 -04:00
|
|
|
isc_once_do(&isc_hash_once, isc_hash_initialize);
|
2020-07-16 11:29:44 -04:00
|
|
|
|
2022-06-27 04:13:19 -04:00
|
|
|
isc_halfsiphash24(isc_hash_key, data, length, case_sensitive,
|
|
|
|
|
(uint8_t *)&hval);
|
2020-07-16 11:29:44 -04:00
|
|
|
|
|
|
|
|
return (hval);
|
|
|
|
|
}
|