Path: blob/main/crypto/openssl/include/internal/hashtable.h
103971 views
/*1* Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OPENSSL_HASHTABLE_H10#define OPENSSL_HASHTABLE_H11#pragma once1213#include <stddef.h>14#include <stdint.h>15#include <openssl/e_os2.h>16#include <internal/rcu.h>17#include "crypto/context.h"1819typedef struct ht_internal_st HT;2021/*22* Represents a key to a hashtable23*/24typedef struct ht_key_header_st {25size_t keysize;26uint8_t *keybuf;27} HT_KEY;2829/*30* Represents a value in the hash table31*/32typedef struct ht_value_st {33void *value;34uintptr_t *type_id;35HT_KEY key;36} HT_VALUE;3738/*39* Represents a list of values filtered from a hash table40*/41typedef struct ht_value_list_st {42size_t list_len;43HT_VALUE **list;44} HT_VALUE_LIST;4546/*47* Hashtable configuration48*/49typedef struct ht_config_st {50OSSL_LIB_CTX *ctx;51void (*ht_free_fn)(HT_VALUE *obj);52uint64_t (*ht_hash_fn)(uint8_t *key, size_t keylen);53size_t init_neighborhoods;54uint32_t collision_check;55uint32_t lockless_reads;56} HT_CONFIG;5758/*59* Hashtable key rules60* Any struct can be used to formulate a hash table key, as long as the61* following rules62* 1) The first element of the struct defining the key must be an HT_KEY63* 2) All struct elements must have a compile time defined length64* 3) Pointers can be used, but the value of the pointer, rather than65* the contents of the address it points to will be used to compute66* the hash67* The key definition macros will assist with enforcing these rules68*/6970/*71* Starts the definition of a hash table key72*/73#define HT_START_KEY_DEFN(keyname) \74typedef struct keyname##_st { \75HT_KEY key_header; \76struct {7778/*79* Ends a hash table key definitions80*/81#define HT_END_KEY_DEFN(keyname) \82} \83keyfields; \84} \85keyname;8687/*88* Defines a field in a hash table key89*/90#define HT_DEF_KEY_FIELD(name, type) type name;9192/*93* convenience macro to define a static char94* array field in a hash table key95*/96#define HT_DEF_KEY_FIELD_CHAR_ARRAY(name, size) \97HT_DEF_KEY_FIELD(name[size], char)9899/*100* Defines a uint8_t (blob) field in a hash table key101*/102#define HT_DEF_KEY_FIELD_UINT8T_ARRAY(name, size) \103HT_DEF_KEY_FIELD(name[size], uint8_t)104105/*106* Initializes a key107*/108#define HT_INIT_KEY(key) \109do { \110memset((key), 0, sizeof(*(key))); \111(key)->key_header.keysize = (sizeof(*(key)) - sizeof(HT_KEY)); \112(key)->key_header.keybuf = (((uint8_t *)key) + sizeof(HT_KEY)); \113} while (0)114115/*116* Resets a hash table key to a known state117*/118#define HT_KEY_RESET(key) memset((key)->key_header.keybuf, 0, (key)->key_header.keysize)119120/*121* Sets a scalar field in a hash table key122*/123#define HT_SET_KEY_FIELD(key, member, value) (key)->keyfields.member = value;124125/*126* Sets a string field in a hash table key, preserving127* null terminator128*/129#define HT_SET_KEY_STRING(key, member, value) \130do { \131if ((value) != NULL) \132strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \133} while (0)134135/*136* This is the same as HT_SET_KEY_STRING, except that it uses137* ossl_ht_strcase to make the value being passed case insensitive138* This is useful for instances in which we want upper and lower case139* key value to hash to the same entry140*/141#define HT_SET_KEY_STRING_CASE(key, member, value) \142do { \143ossl_ht_strcase((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \144} while (0)145146/*147* Same as HT_SET_KEY_STRING but also takes length of the string.148*/149#define HT_SET_KEY_STRING_N(key, member, value, len) \150do { \151if ((value) != NULL) { \152if (len < sizeof((key)->keyfields.member)) \153strncpy((key)->keyfields.member, value, len); \154else \155strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \156} \157} while (0)158159/* Same as HT_SET_KEY_STRING_CASE but also takes length of the string. */160#define HT_SET_KEY_STRING_CASE_N(key, member, value, len) \161do { \162if (len < sizeof((key)->keyfields.member)) \163ossl_ht_strcase((key)->keyfields.member, value, len); \164else \165ossl_ht_strcase((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \166} while (0)167168/*169* Sets a uint8_t (blob) field in a hash table key170*/171#define HT_SET_KEY_BLOB(key, member, value, len) \172do { \173if (value != NULL) \174memcpy((key)->keyfields.member, value, len); \175} while (0)176177/*178* Converts a defined key type to an HT_KEY179*/180#define TO_HT_KEY(key) &(key)->key_header181182/*183* Converts an HT_KEY back to its defined184* type185*/186#define FROM_HT_KEY(key, type) (type)(key)187188/*189* Implements the following type safe operations for a hash table190* ossl_ht_NAME_TYPE_insert - insert a value to a hash table of type TYPE191* ossl_ht_NAME_TYPE_get - gets a value of a specific type from the hash table192* ossl_ht_NAME_TYPE_from_value - converts an HT_VALUE to its type193* ossl_ht_NAME_TYPE_to_value - converts a TYPE to an HT_VALUE194* ossl_ht_NAME_TYPE_type - boolean to detect if a value is of TYPE195*/196#define IMPLEMENT_HT_VALUE_TYPE_FNS(vtype, name, pfx) \197static uintptr_t name##_##vtype##_id = 0; \198pfx ossl_unused int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, \199vtype *data, \200vtype **olddata) \201{ \202HT_VALUE inval; \203HT_VALUE *oval = NULL; \204int rc; \205\206inval.value = data; \207inval.type_id = &name##_##vtype##_id; \208rc = ossl_ht_insert(h, key, &inval, olddata == NULL ? NULL : &oval); \209if (oval != NULL) \210*olddata = (vtype *)oval->value; \211return rc; \212} \213\214pfx ossl_unused vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v) \215{ \216uintptr_t *expect_type = &name##_##vtype##_id; \217if (v == NULL) \218return NULL; \219if (v->type_id != expect_type) \220return NULL; \221return (vtype *)v->value; \222} \223\224pfx ossl_unused vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \225HT_KEY *key, \226HT_VALUE **v) \227{ \228HT_VALUE *vv; \229vv = ossl_ht_get(h, key); \230if (vv == NULL) \231return NULL; \232*v = ossl_rcu_deref(&vv); \233return ossl_ht_##name##_##vtype##_from_value(*v); \234} \235\236pfx ossl_unused HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, \237HT_VALUE *v) \238{ \239v->type_id = &name##_##vtype##_id; \240v->value = data; \241return v; \242} \243\244pfx ossl_unused int ossl_ht_##name##_##vtype##_type(HT_VALUE *h) \245{ \246return h->type_id == &name##_##vtype##_id; \247}248249#define DECLARE_HT_VALUE_TYPE_FNS(vtype, name) \250int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, vtype *data, \251vtype **olddata); \252vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v); \253vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h, \254HT_KEY *key, \255HT_VALUE **v); \256HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, HT_VALUE *v); \257int ossl_ht_##name##_##vtype##_type(HT_VALUE *h);258259/*260* Helper function to construct case insensitive keys261*/262static void ossl_unused ossl_ht_strcase(char *tgt, const char *src, int len)263{264int i;265#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)266const long int case_adjust = ~0x40;267#else268const long int case_adjust = ~0x20;269#endif270271if (src == NULL)272return;273274for (i = 0; src[i] != '\0' && i < len; i++)275tgt[i] = case_adjust & src[i];276}277278/*279* Create a new hashtable280*/281HT *ossl_ht_new(const HT_CONFIG *conf);282283/*284* Frees a hash table, potentially freeing all elements285*/286void ossl_ht_free(HT *htable);287288/*289* Lock the table for reading290*/291void ossl_ht_read_lock(HT *htable);292293/*294* Lock the table for writing295*/296void ossl_ht_write_lock(HT *htable);297298/*299* Read unlock300*/301void ossl_ht_read_unlock(HT *htable);302303/*304* Write unlock305*/306void ossl_ht_write_unlock(HT *htable);307308/*309* Empties a hash table, potentially freeing all elements310*/311int ossl_ht_flush(HT *htable);312313/*314* Inserts an element to a hash table, optionally returning315* replaced data to caller316* Returns 1 if the insert was successful, 0 on error317*/318int ossl_ht_insert(HT *htable, HT_KEY *key, HT_VALUE *data,319HT_VALUE **olddata);320321/*322* Deletes a value from a hash table, based on key323* Returns 1 if the key was removed, 0 if they key was not found324*/325int ossl_ht_delete(HT *htable, HT_KEY *key);326327/*328* Returns number of elements in the hash table329*/330size_t ossl_ht_count(HT *htable);331332/*333* Iterates over each element in the table.334* aborts the loop when cb returns 0335* Contents of elements in the list may be modified during336* this traversal, assuming proper thread safety is observed while doing337* so (holding the table write lock is sufficient). However, elements of the338* table may not be inserted or removed while iterating.339*/340void ossl_ht_foreach_until(HT *htable, int (*cb)(HT_VALUE *obj, void *arg),341void *arg);342/*343* Returns a list of elements in a hash table based on344* filter function return value. Returns NULL on error,345* or an HT_VALUE_LIST object on success. Note it is possible346* That a list will be returned with 0 entries, if none were found.347* The zero length list must still be freed via ossl_ht_value_list_free348*/349HT_VALUE_LIST *ossl_ht_filter(HT *htable, size_t max_len,350int (*filter)(HT_VALUE *obj, void *arg),351void *arg);352/*353* Frees the list returned from ossl_ht_filter354*/355void ossl_ht_value_list_free(HT_VALUE_LIST *list);356357/*358* Fetches a value from the hash table, based359* on key. Returns NULL if the element was not found.360*/361HT_VALUE *ossl_ht_get(HT *htable, HT_KEY *key);362363#endif364365366