/src/bind9/lib/isc/hash.c
Line | Count | Source (jump to first uncovered line) |
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/entropy.h> |
20 | | #include <isc/hash.h> /* IWYU pragma: keep */ |
21 | | #include <isc/random.h> |
22 | | #include <isc/result.h> |
23 | | #include <isc/siphash.h> |
24 | | #include <isc/string.h> |
25 | | #include <isc/types.h> |
26 | | #include <isc/util.h> |
27 | | |
28 | | static uint8_t isc_hash_key[16]; |
29 | | |
30 | | void |
31 | 22 | isc__hash_initialize(void) { |
32 | | /* |
33 | | * Set a constant key to help in problem reproduction should |
34 | | * fuzzing find a crash or a hang. |
35 | | */ |
36 | 22 | uint8_t key[16] = { 1 }; |
37 | | #if !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
38 | | isc_entropy_get(key, sizeof(key)); |
39 | | #endif /* if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
40 | 22 | STATIC_ASSERT(sizeof(key) >= sizeof(isc_hash_key), |
41 | 22 | "sizeof(key) < sizeof(isc_hash_key)"); |
42 | 22 | memmove(isc_hash_key, key, sizeof(isc_hash_key)); |
43 | 22 | } |
44 | | |
45 | | const void * |
46 | 0 | isc_hash_get_initializer(void) { |
47 | 0 | return isc_hash_key; |
48 | 0 | } |
49 | | |
50 | | void |
51 | 0 | isc_hash_set_initializer(const void *initializer) { |
52 | 0 | REQUIRE(initializer != NULL); |
53 | |
|
54 | 0 | memmove(isc_hash_key, initializer, sizeof(isc_hash_key)); |
55 | 0 | } |
56 | | |
57 | | void |
58 | 560k | isc_hash32_init(isc_hash32_t *restrict state) { |
59 | 560k | isc_halfsiphash24_init(state, isc_hash_key); |
60 | 560k | } |
61 | | |
62 | | void |
63 | | isc_hash32_hash(isc_hash32_t *restrict state, const void *data, |
64 | 1.09M | const size_t length, const bool case_sensitive) { |
65 | 1.09M | REQUIRE(length == 0 || data != NULL); |
66 | | |
67 | 1.09M | isc_halfsiphash24_hash(state, data, length, case_sensitive); |
68 | 1.09M | } |
69 | | |
70 | | uint32_t |
71 | 560k | isc_hash32_finalize(isc_hash32_t *restrict state) { |
72 | 560k | uint32_t hval; |
73 | | |
74 | 560k | isc_halfsiphash24_finalize(state, (uint8_t *)&hval); |
75 | | |
76 | 560k | return hval; |
77 | 560k | } |
78 | | |
79 | | void |
80 | 0 | isc_hash64_init(isc_hash64_t *restrict state) { |
81 | 0 | isc_siphash24_init(state, isc_hash_key); |
82 | 0 | } |
83 | | |
84 | | void |
85 | | isc_hash64_hash(isc_hash64_t *restrict state, const void *data, |
86 | 0 | const size_t length, const bool case_sensitive) { |
87 | 0 | REQUIRE(length == 0 || data != NULL); |
88 | |
|
89 | 0 | isc_siphash24_hash(state, data, length, case_sensitive); |
90 | 0 | } |
91 | | |
92 | | uint64_t |
93 | 0 | isc_hash64_finalize(isc_hash64_t *restrict state) { |
94 | 0 | uint64_t hval; |
95 | |
|
96 | 0 | isc_siphash24_finalize(state, (uint8_t *)&hval); |
97 | |
|
98 | 0 | return hval; |
99 | 0 | } |