Path: blob/main/crypto/krb5/src/include/k5-hashtab.h
34879 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* include/k5-hash.h - hash table interface definitions */2/*3* Copyright (C) 2018 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/*33* This file contains declarations for a simple hash table using siphash. Some34* limitations which might need to be addressed in the future:35*36* - The table does not manage caller memory. This limitation could be37* addressed by adding an optional free callback to k5_hashtab_create(), to38* be called by k5_hashtab_free() and k5_hashtab_remove().39*40* - There is no way to iterate over a hash table.41*42* - k5_hashtab_add() does not check for duplicate entries.43*/4445#ifndef K5_HASH_H46#define K5_HASH_H4748#define K5_HASH_SEED_LEN 164950struct k5_hashtab;5152/*53* Create a new hash table in *ht_out. seed must point to random bytes if keys54* might be under the control of an attacker; otherwise it may be NULL.55* initial_buckets controls the initial allocation of hash buckets; pass zero56* to use a default value. The number of hash buckets will be doubled as the57* number of entries increases. Return 0 on success, ENOMEM on failure.58*/59int k5_hashtab_create(const uint8_t seed[K5_HASH_SEED_LEN],60size_t initial_buckets, struct k5_hashtab **ht_out);6162/* Release the memory used by a hash table. Keys and values are the caller's63* responsibility. */64void k5_hashtab_free(struct k5_hashtab *ht);6566/* Add an entry to a hash table. key and val must remain valid until the entry67* is removed or the hash table is freed. The caller must avoid duplicates. */68int k5_hashtab_add(struct k5_hashtab *ht, const void *key, size_t klen,69void *val);7071/* Remove an entry from a hash table by key. Does not free key or the72* associated value. Return 1 if the key was found and removed, 0 if not. */73int k5_hashtab_remove(struct k5_hashtab *ht, const void *key, size_t klen);7475/* Retrieve a value from a hash table by key. */76void *k5_hashtab_get(struct k5_hashtab *ht, const void *key, size_t klen);7778uint64_t k5_siphash24(const uint8_t *data, size_t len,79const uint8_t seed[K5_HASH_SEED_LEN]);8081#endif /* K5_HASH_H */828384