2003-07-25 02:22:26 +00:00
|
|
|
/*
|
2009-05-06 23:47:50 +00:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2003-07-25 02:22:26 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2003-07-25 02:22:26 +00: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 09:53:12 +01:00
|
|
|
*
|
2003-07-25 02:22:26 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 13:59:57 +02:00
|
|
|
#include <stddef.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2022-09-22 10:36:53 +02:00
|
|
|
#include <isc/ascii.h>
|
|
|
|
#include <isc/entropy.h>
|
|
|
|
#include <isc/hash.h> /* IWYU pragma: keep */
|
|
|
|
#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-25 02:22:26 +00:00
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
static uint8_t isc_hash_key[16];
|
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
void
|
|
|
|
isc__hash_initialize(void) {
|
2019-04-04 13:51:09 +02:00
|
|
|
/*
|
|
|
|
* Set a constant key to help in problem reproduction should
|
|
|
|
* fuzzing find a crash or a hang.
|
|
|
|
*/
|
2023-09-08 17:48:45 +02:00
|
|
|
uint8_t key[16] = { 1 };
|
2020-07-16 17:29:44 +02:00
|
|
|
#if !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
2019-04-04 13:51:09 +02:00
|
|
|
isc_entropy_get(key, sizeof(key));
|
|
|
|
#endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|
2023-09-08 17:48:45 +02:00
|
|
|
STATIC_ASSERT(sizeof(key) >= sizeof(isc_hash_key),
|
|
|
|
"sizeof(key) < sizeof(isc_hash_key)");
|
2019-04-04 13:51:09 +02:00
|
|
|
memmove(isc_hash_key, key, sizeof(isc_hash_key));
|
|
|
|
}
|
|
|
|
|
2016-02-12 00:22:45 -08:00
|
|
|
const void *
|
|
|
|
isc_hash_get_initializer(void) {
|
2019-04-04 13:51:09 +02:00
|
|
|
return isc_hash_key;
|
2016-02-12 00:22:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_hash_set_initializer(const void *initializer) {
|
|
|
|
REQUIRE(initializer != NULL);
|
|
|
|
|
2019-04-04 13:51:09 +02:00
|
|
|
memmove(isc_hash_key, initializer, sizeof(isc_hash_key));
|
2016-02-12 00:22:45 -08:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
void
|
|
|
|
isc_hash32_init(isc_hash32_t *restrict state) {
|
|
|
|
isc_halfsiphash24_init(state, isc_hash_key);
|
|
|
|
}
|
2015-12-09 19:07:20 +05:30
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
void
|
|
|
|
isc_hash32_hash(isc_hash32_t *restrict state, const void *data,
|
|
|
|
const size_t length, const bool case_sensitive) {
|
2016-08-29 18:52:49 +05:30
|
|
|
REQUIRE(length == 0 || data != NULL);
|
2017-04-22 08:25:10 +05:30
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
isc_halfsiphash24_hash(state, data, length, case_sensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
isc_hash32_finalize(isc_hash32_t *restrict state) {
|
|
|
|
uint32_t hval;
|
2015-12-09 19:07:20 +05:30
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
isc_halfsiphash24_finalize(state, (uint8_t *)&hval);
|
2015-12-09 19:07:20 +05:30
|
|
|
|
|
|
|
return hval;
|
|
|
|
}
|
2020-07-16 17:29:44 +02:00
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
void
|
|
|
|
isc_hash64_init(isc_hash64_t *restrict state) {
|
|
|
|
isc_siphash24_init(state, isc_hash_key);
|
|
|
|
}
|
2020-07-16 17:29:44 +02:00
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
void
|
|
|
|
isc_hash64_hash(isc_hash64_t *restrict state, const void *data,
|
|
|
|
const size_t length, const bool case_sensitive) {
|
2020-07-16 17:29:44 +02:00
|
|
|
REQUIRE(length == 0 || data != NULL);
|
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
isc_siphash24_hash(state, data, length, case_sensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
isc_hash64_finalize(isc_hash64_t *restrict state) {
|
|
|
|
uint64_t hval;
|
2020-07-16 17:29:44 +02:00
|
|
|
|
2023-09-08 17:48:45 +02:00
|
|
|
isc_siphash24_finalize(state, (uint8_t *)&hval);
|
2020-07-16 17:29:44 +02:00
|
|
|
|
|
|
|
return hval;
|
|
|
|
}
|