OpenVPN
list.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "syshead.h"
29
30
31#include "integer.h"
32#include "list.h"
33#include "misc.h"
34
35#include "memdbg.h"
36
37struct hash *
39 const uint32_t iv,
40 uint32_t (*hash_function)(const void *key, uint32_t iv),
41 bool (*compare_function)(const void *key1, const void *key2))
42{
43 struct hash *h;
44 int i;
45
46 ASSERT(n_buckets > 0);
47 ALLOC_OBJ_CLEAR(h, struct hash);
49 h->mask = h->n_buckets - 1;
52 h->iv = iv;
54 for (i = 0; i < h->n_buckets; ++i)
55 {
56 struct hash_bucket *b = &h->buckets[i];
57 b->list = NULL;
58 }
59 return h;
60}
61
62void
64{
65 int i;
66 for (i = 0; i < hash->n_buckets; ++i)
67 {
68 struct hash_bucket *b = &hash->buckets[i];
69 struct hash_element *he = b->list;
70
71 while (he)
72 {
73 struct hash_element *next = he->next;
74 free(he);
75 he = next;
76 }
77 }
78 free(hash->buckets);
79 free(hash);
80}
81
82struct hash_element *
84 struct hash_bucket *bucket,
85 const void *key,
86 uint32_t hv)
87{
88 struct hash_element *he;
89 struct hash_element *prev = NULL;
90
91 he = bucket->list;
92
93 while (he)
94 {
95 if (hv == he->hash_value && (*hash->compare_function)(key, he->key))
96 {
97 /* move to head of list */
98 if (prev)
99 {
100 prev->next = he->next;
101 he->next = bucket->list;
102 bucket->list = he;
103 }
104 return he;
105 }
106 prev = he;
107 he = he->next;
108 }
109
110 return NULL;
111}
112
113bool
115 struct hash_bucket *bucket,
116 const void *key,
117 uint32_t hv)
118{
119 struct hash_element *he;
120 struct hash_element *prev = NULL;
121
122 he = bucket->list;
123
124 while (he)
125 {
126 if (hv == he->hash_value && (*hash->compare_function)(key, he->key))
127 {
128 if (prev)
129 {
130 prev->next = he->next;
131 }
132 else
133 {
134 bucket->list = he->next;
135 }
136 free(he);
137 --hash->n_elements;
138 return true;
139 }
140 prev = he;
141 he = he->next;
142 }
143 return false;
144}
145
146bool
147hash_add(struct hash *hash, const void *key, void *value, bool replace)
148{
149 uint32_t hv;
150 struct hash_bucket *bucket;
151 struct hash_element *he;
152 bool ret = false;
153
154 hv = hash_value(hash, key);
155 bucket = &hash->buckets[hv & hash->mask];
156
157 if ((he = hash_lookup_fast(hash, bucket, key, hv))) /* already exists? */
158 {
159 if (replace)
160 {
161 he->value = value;
162 ret = true;
163 }
164 }
165 else
166 {
167 hash_add_fast(hash, bucket, key, hv, value);
168 ret = true;
169 }
170
171 return ret;
172}
173
174void
176{
177 struct hash_iterator hi;
178 struct hash_element *he;
179
181 while ((he = hash_iterator_next(&hi)))
182 {
183 if (he->value == value)
184 {
186 }
187 }
189}
190
191static void
192hash_remove_marked(struct hash *hash, struct hash_bucket *bucket)
193{
194 struct hash_element *prev = NULL;
195 struct hash_element *he = bucket->list;
196
197 while (he)
198 {
199 if (!he->key) /* marked? */
200 {
201 struct hash_element *newhe;
202 if (prev)
203 {
204 newhe = prev->next = he->next;
205 }
206 else
207 {
208 newhe = bucket->list = he->next;
209 }
210 free(he);
211 --hash->n_elements;
212 he = newhe;
213 }
214 else
215 {
216 prev = he;
217 he = he->next;
218 }
219 }
220}
221
222void
224 struct hash_iterator *hi,
225 int start_bucket,
226 int end_bucket)
227{
228 if (end_bucket > hash->n_buckets)
229 {
230 end_bucket = hash->n_buckets;
231 }
232
233 ASSERT(start_bucket >= 0 && start_bucket <= end_bucket);
234
235 hi->hash = hash;
236 hi->elem = NULL;
237 hi->bucket = NULL;
238 hi->last = NULL;
239 hi->bucket_marked = false;
240 hi->bucket_index_start = start_bucket;
241 hi->bucket_index_end = end_bucket;
242 hi->bucket_index = hi->bucket_index_start - 1;
243}
244
245void
247 struct hash_iterator *hi)
248{
250}
251
252static inline void
254{
255 hi->bucket = b;
256 hi->last = NULL;
257 hi->bucket_marked = false;
258}
259
260static inline void
262{
263 if (hi->bucket)
264 {
265 if (hi->bucket_marked)
266 {
268 hi->bucket_marked = false;
269 }
270 hi->bucket = NULL;
271 hi->last = NULL;
272 }
273}
274
275static inline void
277{
278 hi->last = hi->elem;
279 hi->elem = hi->elem->next;
280}
281
282void
287
288struct hash_element *
290{
291 struct hash_element *ret = NULL;
292 if (hi->elem)
293 {
294 ret = hi->elem;
296 }
297 else
298 {
299 while (++hi->bucket_index < hi->bucket_index_end)
300 {
301 struct hash_bucket *b;
303 b = &hi->hash->buckets[hi->bucket_index];
304 if (b->list)
305 {
306 hash_iterator_lock(hi, b);
307 hi->elem = b->list;
308 if (hi->elem)
309 {
310 ret = hi->elem;
312 break;
313 }
314 }
315 }
316 }
317 return ret;
318}
319
320void
322{
323 ASSERT(hi->last);
324 hi->last->key = NULL;
325 hi->bucket_marked = true;
326}
327
328
329/*
330 * --------------------------------------------------------------------
331 * hash() -- hash a variable-length key into a 32-bit value
332 * k : the key (the unaligned variable-length array of bytes)
333 * len : the length of the key, counting by bytes
334 * level : can be any 4-byte value
335 * Returns a 32-bit value. Every bit of the key affects every bit of
336 * the return value. Every 1-bit and 2-bit delta achieves avalanche.
337 * About 36+6len instructions.
338 *
339 * The best hash table sizes are powers of 2. There is no need to do
340 * mod a prime (mod is sooo slow!). If you need less than 32 bits,
341 * use a bitmask. For example, if you need only 10 bits, do
342 * h = (h & hashmask(10));
343 * In which case, the hash table should have hashsize(10) elements.
344 *
345 * If you are hashing n strings (uint8_t **)k, do it like this:
346 * for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
347 *
348 * By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
349 * code any way you wish, private, educational, or commercial. It's free.
350 *
351 * See http://burlteburtle.net/bob/hash/evahash.html
352 * Use for hash table lookup, or anything where one collision in 2^32 is
353 * acceptable. Do NOT use for cryptographic purposes.
354 *
355 * --------------------------------------------------------------------
356 *
357 * mix -- mix 3 32-bit values reversibly.
358 * For every delta with one or two bit set, and the deltas of all three
359 * high bits or all three low bits, whether the original value of a,b,c
360 * is almost all zero or is uniformly distributed,
361 * If mix() is run forward or backward, at least 32 bits in a,b,c
362 * have at least 1/4 probability of changing.
363 * If mix() is run forward, every bit of c will change between 1/3 and
364 * 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
365 * mix() was built out of 36 single-cycle latency instructions in a
366 * structure that could supported 2x parallelism, like so:
367 * a -= b;
368 * a -= c; x = (c>>13);
369 * b -= c; a ^= x;
370 * b -= a; x = (a<<8);
371 * c -= a; b ^= x;
372 * c -= b; x = (b>>13);
373 * ...
374 * Unfortunately, superscalar Pentiums and Sparcs can't take advantage
375 * of that parallelism. They've also turned some of those single-cycle
376 * latency instructions into multi-cycle latency instructions. Still,
377 * this is the fastest good hash I could find. There were about 2^^68
378 * to choose from. I only looked at a billion or so.
379 *
380 * James Yonan Notes:
381 *
382 * This function is faster than it looks, and appears to be
383 * appropriate for our usage in OpenVPN which is primarily
384 * for hash-table based address lookup (IPv4, IPv6, and Ethernet MAC).
385 * NOTE: This function is never used for cryptographic purposes, only
386 * to produce evenly-distributed indexes into hash tables.
387 *
388 * Benchmark results: 11.39 machine cycles per byte on a P2 266Mhz,
389 * and 12.1 machine cycles per byte on a
390 * 2.2 Ghz P4 when hashing a 6 byte string.
391 * --------------------------------------------------------------------
392 */
393
394#define mix(a, b, c) \
395 { \
396 a -= b; a -= c; a ^= (c>>13); \
397 b -= c; b -= a; b ^= (a<<8); \
398 c -= a; c -= b; c ^= (b>>13); \
399 a -= b; a -= c; a ^= (c>>12); \
400 b -= c; b -= a; b ^= (a<<16); \
401 c -= a; c -= b; c ^= (b>>5); \
402 a -= b; a -= c; a ^= (c>>3); \
403 b -= c; b -= a; b ^= (a<<10); \
404 c -= a; c -= b; c ^= (b>>15); \
405 }
406
407uint32_t
408hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
409{
410 uint32_t a, b, c, len;
411
412 /* Set up the internal state */
413 len = length;
414 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
415 c = initval; /* the previous hash value */
416
417 /*---------------------------------------- handle most of the key */
418 while (len >= 12)
419 {
420 a += (k[0] + ((uint32_t) k[1] << 8)
421 + ((uint32_t) k[2] << 16)
422 + ((uint32_t) k[3] << 24));
423 b += (k[4] + ((uint32_t) k[5] << 8)
424 + ((uint32_t) k[6] << 16)
425 + ((uint32_t) k[7] << 24));
426 c += (k[8] + ((uint32_t) k[9] << 8)
427 + ((uint32_t) k[10] << 16)
428 + ((uint32_t) k[11] << 24));
429 mix(a, b, c);
430 k += 12;
431 len -= 12;
432 }
433
434 /*------------------------------------- handle the last 11 bytes */
435 c += length;
436 switch (len) /* all the case statements fall through */
437 {
438 case 11:
439 c += ((uint32_t) k[10] << 24);
440 /* Intentional [[fallthrough]]; */
441
442 case 10:
443 c += ((uint32_t) k[9] << 16);
444 /* Intentional [[fallthrough]]; */
445
446 case 9:
447 c += ((uint32_t) k[8] << 8);
448 /* Intentional [[fallthrough]]; */
449
450 /* the first byte of c is reserved for the length */
451 case 8:
452 b += ((uint32_t) k[7] << 24);
453 /* Intentional [[fallthrough]]; */
454
455 case 7:
456 b += ((uint32_t) k[6] << 16);
457 /* Intentional [[fallthrough]]; */
458
459 case 6:
460 b += ((uint32_t) k[5] << 8);
461 /* Intentional [[fallthrough]]; */
462
463 case 5:
464 b += k[4];
465 /* Intentional [[fallthrough]]; */
466
467 case 4:
468 a += ((uint32_t) k[3] << 24);
469 /* Intentional [[fallthrough]]; */
470
471 case 3:
472 a += ((uint32_t) k[2] << 16);
473 /* Intentional [[fallthrough]]; */
474
475 case 2:
476 a += ((uint32_t) k[1] << 8);
477 /* Intentional [[fallthrough]]; */
478
479 case 1:
480 a += k[0];
481 /* case 0: nothing left to add */
482 }
483 mix(a, b, c);
484 /*-------------------------------------- report the result */
485 return c;
486}
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition buffer.h:1060
#define ALLOC_ARRAY(dptr, type, n)
Definition buffer.h:1066
static const char *const key1
Definition cert_data.h:56
static size_t adjust_power_of_2(size_t u)
Definition integer.h:181
static void hash_remove_marked(struct hash *hash, struct hash_bucket *bucket)
Definition list.c:192
void hash_iterator_free(struct hash_iterator *hi)
Definition list.c:283
struct hash_element * hash_iterator_next(struct hash_iterator *hi)
Definition list.c:289
void hash_iterator_delete_element(struct hash_iterator *hi)
Definition list.c:321
struct hash_element * hash_lookup_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:83
void hash_iterator_init(struct hash *hash, struct hash_iterator *hi)
Definition list.c:246
struct hash * hash_init(const int n_buckets, const uint32_t iv, uint32_t(*hash_function)(const void *key, uint32_t iv), bool(*compare_function)(const void *key1, const void *key2))
Definition list.c:38
void hash_iterator_init_range(struct hash *hash, struct hash_iterator *hi, int start_bucket, int end_bucket)
Definition list.c:223
static void hash_iterator_lock(struct hash_iterator *hi, struct hash_bucket *b)
Definition list.c:253
bool hash_remove_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv)
Definition list.c:114
static void hash_iterator_unlock(struct hash_iterator *hi)
Definition list.c:261
uint32_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval)
Definition list.c:408
void hash_free(struct hash *hash)
Definition list.c:63
bool hash_add(struct hash *hash, const void *key, void *value, bool replace)
Definition list.c:147
void hash_remove_by_value(struct hash *hash, void *value)
Definition list.c:175
static void hash_iterator_advance(struct hash_iterator *hi)
Definition list.c:276
#define mix(a, b, c)
Definition list.c:394
static uint32_t hash_value(const struct hash *hash, const void *key)
Definition list.h:116
static void hash_add_fast(struct hash *hash, struct hash_bucket *bucket, const void *key, uint32_t hv, void *value)
Definition list.h:158
#define ASSERT(x)
Definition error.h:195
struct hash_element * list
Definition list.h:53
void * value
Definition list.h:45
const void * key
Definition list.h:46
struct hash_element * next
Definition list.h:48
unsigned int hash_value
Definition list.h:47
bool bucket_marked
Definition list.h:95
int bucket_index_end
Definition list.h:97
struct hash_bucket * bucket
Definition list.h:92
struct hash * hash
Definition list.h:90
int bucket_index_start
Definition list.h:96
struct hash_element * elem
Definition list.h:93
int bucket_index
Definition list.h:91
struct hash_element * last
Definition list.h:94
Definition list.h:57
uint32_t(* hash_function)(const void *key, uint32_t iv)
Definition list.h:62
struct hash_bucket * buckets
Definition list.h:64
int n_buckets
Definition list.h:58
int mask
Definition list.h:60
uint32_t iv
Definition list.h:61
bool(* compare_function)(const void *key1, const void *key2)
Definition list.h:63
int n_elements
Definition list.h:59
Container for bidirectional cipher and HMAC key material.
Definition crypto.h:239
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152