/src/bind9/lib/isc/hash.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | #include <inttypes.h> |
15 | | #include <stdbool.h> |
16 | | #include <stddef.h> |
17 | | |
18 | | #include <isc/ascii.h> |
19 | | #include <isc/hash.h> /* IWYU pragma: keep */ |
20 | | #include <isc/random.h> |
21 | | #include <isc/result.h> |
22 | | #include <isc/siphash.h> |
23 | | #include <isc/string.h> |
24 | | #include <isc/types.h> |
25 | | #include <isc/util.h> |
26 | | |
27 | | static uint8_t isc_hash_key[16]; |
28 | | |
29 | | void |
30 | 2 | isc__hash_initialize(void) { |
31 | | /* |
32 | | * Set a constant key to help in problem reproduction should |
33 | | * fuzzing find a crash or a hang. |
34 | | */ |
35 | 2 | uint8_t key[16] = { 1 }; |
36 | | #if !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
37 | | isc_random_buf(key, sizeof(key)); |
38 | | #endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
39 | 2 | STATIC_ASSERT(sizeof(key) >= sizeof(isc_hash_key), |
40 | 2 | "sizeof(key) < sizeof(isc_hash_key)"); |
41 | 2 | memmove(isc_hash_key, key, sizeof(isc_hash_key)); |
42 | 2 | } |
43 | | |
44 | | const void * |
45 | 0 | isc_hash_get_initializer(void) { |
46 | 0 | return isc_hash_key; |
47 | 0 | } |
48 | | |
49 | | void |
50 | 0 | isc_hash_set_initializer(const void *initializer) { |
51 | 0 | REQUIRE(initializer != NULL); |
52 | |
|
53 | 0 | memmove(isc_hash_key, initializer, sizeof(isc_hash_key)); |
54 | 0 | } |
55 | | |
56 | | void |
57 | 0 | isc_hash32_init(isc_hash32_t *restrict state) { |
58 | 0 | isc_halfsiphash24_init(state, isc_hash_key); |
59 | 0 | } |
60 | | |
61 | | void |
62 | | isc_hash32_hash(isc_hash32_t *restrict state, const void *data, |
63 | 0 | const size_t length, const bool case_sensitive) { |
64 | 0 | REQUIRE(length == 0 || data != NULL); |
65 | |
|
66 | 0 | isc_halfsiphash24_hash(state, data, length, case_sensitive); |
67 | 0 | } |
68 | | |
69 | | uint32_t |
70 | 0 | isc_hash32_finalize(isc_hash32_t *restrict state) { |
71 | 0 | uint32_t hval; |
72 | |
|
73 | 0 | isc_halfsiphash24_finalize(state, (uint8_t *)&hval); |
74 | |
|
75 | 0 | return hval; |
76 | 0 | } |
77 | | |
78 | | void |
79 | 0 | isc_hash64_init(isc_hash64_t *restrict state) { |
80 | 0 | isc_siphash24_init(state, isc_hash_key); |
81 | 0 | } |
82 | | |
83 | | void |
84 | | isc_hash64_hash(isc_hash64_t *restrict state, const void *data, |
85 | 0 | const size_t length, const bool case_sensitive) { |
86 | 0 | REQUIRE(length == 0 || data != NULL); |
87 | |
|
88 | 0 | isc_siphash24_hash(state, data, length, case_sensitive); |
89 | 0 | } |
90 | | |
91 | | uint64_t |
92 | 0 | isc_hash64_finalize(isc_hash64_t *restrict state) { |
93 | 0 | uint64_t hval; |
94 | |
|
95 | 0 | isc_siphash24_finalize(state, (uint8_t *)&hval); |
96 | |
|
97 | 0 | return hval; |
98 | 0 | } |