]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - secure/lib/libcrypto/man/lhash.3
Fix multiple OpenSSL vulnerabilities.
[FreeBSD/releng/9.3.git] / secure / lib / libcrypto / man / lhash.3
1 .\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 .    ds C`
42 .    ds C'
43 'br\}
44 .\"
45 .\" Escape single quotes in literal strings from groff's Unicode transform.
46 .ie \n(.g .ds Aq \(aq
47 .el       .ds Aq '
48 .\"
49 .\" If the F register is turned on, we'll generate index entries on stderr for
50 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
51 .\" entries marked with X<> in POD.  Of course, you'll have to process the
52 .\" output yourself in some meaningful fashion.
53 .\"
54 .\" Avoid warning from groff about undefined register 'F'.
55 .de IX
56 ..
57 .nr rF 0
58 .if \n(.g .if rF .nr rF 1
59 .if (\n(rF:(\n(.g==0)) \{
60 .    if \nF \{
61 .        de IX
62 .        tm Index:\\$1\t\\n%\t"\\$2"
63 ..
64 .        if !\nF==2 \{
65 .            nr % 0
66 .            nr F 2
67 .        \}
68 .    \}
69 .\}
70 .rr rF
71 .\"
72 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
73 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
74 .    \" fudge factors for nroff and troff
75 .if n \{\
76 .    ds #H 0
77 .    ds #V .8m
78 .    ds #F .3m
79 .    ds #[ \f1
80 .    ds #] \fP
81 .\}
82 .if t \{\
83 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
84 .    ds #V .6m
85 .    ds #F 0
86 .    ds #[ \&
87 .    ds #] \&
88 .\}
89 .    \" simple accents for nroff and troff
90 .if n \{\
91 .    ds ' \&
92 .    ds ` \&
93 .    ds ^ \&
94 .    ds , \&
95 .    ds ~ ~
96 .    ds /
97 .\}
98 .if t \{\
99 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
100 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
101 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
102 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
103 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
104 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
105 .\}
106 .    \" troff and (daisy-wheel) nroff accents
107 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
108 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
109 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
110 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
111 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
112 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
113 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
114 .ds ae a\h'-(\w'a'u*4/10)'e
115 .ds Ae A\h'-(\w'A'u*4/10)'E
116 .    \" corrections for vroff
117 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
118 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
119 .    \" for low resolution devices (crt and lpr)
120 .if \n(.H>23 .if \n(.V>19 \
121 \{\
122 .    ds : e
123 .    ds 8 ss
124 .    ds o a
125 .    ds d- d\h'-1'\(ga
126 .    ds D- D\h'-1'\(hy
127 .    ds th \o'bp'
128 .    ds Th \o'LP'
129 .    ds ae ae
130 .    ds Ae AE
131 .\}
132 .rm #[ #] #H #V #F C
133 .\" ========================================================================
134 .\"
135 .IX Title "lhash 3"
136 .TH lhash 3 "2015-12-03" "0.9.8zh" "OpenSSL"
137 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
138 .\" way too many mistakes in technical documents.
139 .if n .ad l
140 .nh
141 .SH "NAME"
142 lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error \- dynamic hash table
143 .SH "SYNOPSIS"
144 .IX Header "SYNOPSIS"
145 .Vb 1
146 \& #include <openssl/lhash.h>
147 \&
148 \& LHASH *lh_new(LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE compare);
149 \& void lh_free(LHASH *table);
150 \&
151 \& void *lh_insert(LHASH *table, void *data);
152 \& void *lh_delete(LHASH *table, void *data);
153 \& void *lh_retrieve(LHASH *table, void *data);
154 \&
155 \& void lh_doall(LHASH *table, LHASH_DOALL_FN_TYPE func);
156 \& void lh_doall_arg(LHASH *table, LHASH_DOALL_ARG_FN_TYPE func,
157 \&          void *arg);
158 \&
159 \& int lh_error(LHASH *table);
160 \&
161 \& typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
162 \& typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
163 \& typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
164 \& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
165 .Ve
166 .SH "DESCRIPTION"
167 .IX Header "DESCRIPTION"
168 This library implements dynamic hash tables. The hash table entries
169 can be arbitrary structures. Usually they consist of key and value
170 fields.
171 .PP
172 \&\fIlh_new()\fR creates a new \fB\s-1LHASH\s0\fR structure to store arbitrary data
173 entries, and provides the 'hash' and 'compare' callbacks to be used in
174 organising the table's entries.  The \fBhash\fR callback takes a pointer
175 to a table entry as its argument and returns an unsigned long hash
176 value for its key field.  The hash value is normally truncated to a
177 power of 2, so make sure that your hash function returns well mixed
178 low order bits.  The \fBcompare\fR callback takes two arguments (pointers
179 to two hash table entries), and returns 0 if their keys are equal,
180 non-zero otherwise.  If your hash table will contain items of some
181 particular type and the \fBhash\fR and \fBcompare\fR callbacks hash/compare
182 these types, then the \fB\s-1DECLARE_LHASH_HASH_FN\s0\fR and
183 \&\fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be used to create callback
184 wrappers of the prototypes required by \fIlh_new()\fR.  These provide
185 per-variable casts before calling the type-specific callbacks written
186 by the application author.  These macros, as well as those used for
187 the \*(L"doall\*(R" callbacks, are defined as;
188 .PP
189 .Vb 7
190 \& #define DECLARE_LHASH_HASH_FN(f_name,o_type) \e
191 \&         unsigned long f_name##_LHASH_HASH(const void *);
192 \& #define IMPLEMENT_LHASH_HASH_FN(f_name,o_type) \e
193 \&         unsigned long f_name##_LHASH_HASH(const void *arg) { \e
194 \&                 o_type a = (o_type)arg; \e
195 \&                 return f_name(a); }
196 \& #define LHASH_HASH_FN(f_name) f_name##_LHASH_HASH
197 \&
198 \& #define DECLARE_LHASH_COMP_FN(f_name,o_type) \e
199 \&         int f_name##_LHASH_COMP(const void *, const void *);
200 \& #define IMPLEMENT_LHASH_COMP_FN(f_name,o_type) \e
201 \&         int f_name##_LHASH_COMP(const void *arg1, const void *arg2) { \e
202 \&                 o_type a = (o_type)arg1; \e
203 \&                 o_type b = (o_type)arg2; \e
204 \&                 return f_name(a,b); }
205 \& #define LHASH_COMP_FN(f_name) f_name##_LHASH_COMP
206 \&
207 \& #define DECLARE_LHASH_DOALL_FN(f_name,o_type) \e
208 \&         void f_name##_LHASH_DOALL(const void *);
209 \& #define IMPLEMENT_LHASH_DOALL_FN(f_name,o_type) \e
210 \&         void f_name##_LHASH_DOALL(const void *arg) { \e
211 \&                 o_type a = (o_type)arg; \e
212 \&                 f_name(a); }
213 \& #define LHASH_DOALL_FN(f_name) f_name##_LHASH_DOALL
214 \&
215 \& #define DECLARE_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e
216 \&         void f_name##_LHASH_DOALL_ARG(const void *, const void *);
217 \& #define IMPLEMENT_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e
218 \&         void f_name##_LHASH_DOALL_ARG(const void *arg1, const void *arg2) { \e
219 \&                 o_type a = (o_type)arg1; \e
220 \&                 a_type b = (a_type)arg2; \e
221 \&                 f_name(a,b); }
222 \& #define LHASH_DOALL_ARG_FN(f_name) f_name##_LHASH_DOALL_ARG
223 .Ve
224 .PP
225 An example of a hash table storing (pointers to) structures of type '\s-1STUFF\s0'
226 could be defined as follows;
227 .PP
228 .Vb 10
229 \& /* Calculates the hash value of \*(Aqtohash\*(Aq (implemented elsewhere) */
230 \& unsigned long STUFF_hash(const STUFF *tohash);
231 \& /* Orders \*(Aqarg1\*(Aq and \*(Aqarg2\*(Aq (implemented elsewhere) */
232 \& int STUFF_cmp(const STUFF *arg1, const STUFF *arg2);
233 \& /* Create the type\-safe wrapper functions for use in the LHASH internals */
234 \& static IMPLEMENT_LHASH_HASH_FN(STUFF_hash, const STUFF *)
235 \& static IMPLEMENT_LHASH_COMP_FN(STUFF_cmp, const STUFF *);
236 \& /* ... */
237 \& int main(int argc, char *argv[]) {
238 \&         /* Create the new hash table using the hash/compare wrappers */
239 \&         LHASH *hashtable = lh_new(LHASH_HASH_FN(STUFF_hash),
240 \&                                   LHASH_COMP_FN(STUFF_cmp));
241 \&         /* ... */
242 \& }
243 .Ve
244 .PP
245 \&\fIlh_free()\fR frees the \fB\s-1LHASH\s0\fR structure \fBtable\fR. Allocated hash table
246 entries will not be freed; consider using \fIlh_doall()\fR to deallocate any
247 remaining entries in the hash table (see below).
248 .PP
249 \&\fIlh_insert()\fR inserts the structure pointed to by \fBdata\fR into \fBtable\fR.
250 If there already is an entry with the same key, the old value is
251 replaced. Note that \fIlh_insert()\fR stores pointers, the data are not
252 copied.
253 .PP
254 \&\fIlh_delete()\fR deletes an entry from \fBtable\fR.
255 .PP
256 \&\fIlh_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR is
257 a structure with the key field(s) set; the function will return a
258 pointer to a fully populated structure.
259 .PP
260 \&\fIlh_doall()\fR will, for every entry in the hash table, call \fBfunc\fR with
261 the data item as its parameter.  For \fIlh_doall()\fR and \fIlh_doall_arg()\fR,
262 function pointer casting should be avoided in the callbacks (see
263 \&\fB\s-1NOTE\s0\fR) \- instead, either declare the callbacks to match the
264 prototype required in \fIlh_new()\fR or use the declare/implement macros to
265 create type-safe wrappers that cast variables prior to calling your
266 type-specific callbacks.  An example of this is illustrated here where
267 the callback is used to cleanup resources for items in the hash table
268 prior to the hashtable itself being deallocated:
269 .PP
270 .Vb 9
271 \& /* Cleans up resources belonging to \*(Aqa\*(Aq (this is implemented elsewhere) */
272 \& void STUFF_cleanup(STUFF *a);
273 \& /* Implement a prototype\-compatible wrapper for "STUFF_cleanup" */
274 \& IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF *)
275 \&         /* ... then later in the code ... */
276 \& /* So to run "STUFF_cleanup" against all items in a hash table ... */
277 \& lh_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
278 \& /* Then the hash table itself can be deallocated */
279 \& lh_free(hashtable);
280 .Ve
281 .PP
282 When doing this, be careful if you delete entries from the hash table
283 in your callbacks: the table may decrease in size, moving the item
284 that you are currently on down lower in the hash table \- this could
285 cause some entries to be skipped during the iteration.  The second
286 best solution to this problem is to set hash\->down_load=0 before
287 you start (which will stop the hash table ever decreasing in size).
288 The best solution is probably to avoid deleting items from the hash
289 table inside a \*(L"doall\*(R" callback!
290 .PP
291 \&\fIlh_doall_arg()\fR is the same as \fIlh_doall()\fR except that \fBfunc\fR will be
292 called with \fBarg\fR as the second argument and \fBfunc\fR should be of
293 type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype that is passed
294 both the table entry and an extra argument).  As with \fIlh_doall()\fR, you
295 can instead choose to declare your callback with a prototype matching
296 the types you are dealing with and use the declare/implement macros to
297 create compatible wrappers that cast variables before calling your
298 type-specific callbacks.  An example of this is demonstrated here
299 (printing all hash table entries to a \s-1BIO\s0 that is provided by the
300 caller):
301 .PP
302 .Vb 7
303 \& /* Prints item \*(Aqa\*(Aq to \*(Aqoutput_bio\*(Aq (this is implemented elsewhere) */
304 \& void STUFF_print(const STUFF *a, BIO *output_bio);
305 \& /* Implement a prototype\-compatible wrapper for "STUFF_print" */
306 \& static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF_print, const STUFF *, BIO *)
307 \&         /* ... then later in the code ... */
308 \& /* Print out the entire hashtable to a particular BIO */
309 \& lh_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), logging_bio);
310 .Ve
311 .PP
312 \&\fIlh_error()\fR can be used to determine if an error occurred in the last
313 operation. \fIlh_error()\fR is a macro.
314 .SH "RETURN VALUES"
315 .IX Header "RETURN VALUES"
316 \&\fIlh_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new
317 \&\fB\s-1LHASH\s0\fR structure.
318 .PP
319 When a hash table entry is replaced, \fIlh_insert()\fR returns the value
320 being replaced. \fB\s-1NULL\s0\fR is returned on normal operation and on error.
321 .PP
322 \&\fIlh_delete()\fR returns the entry being deleted.  \fB\s-1NULL\s0\fR is returned if
323 there is no such value in the hash table.
324 .PP
325 \&\fIlh_retrieve()\fR returns the hash table entry if it has been found,
326 \&\fB\s-1NULL\s0\fR otherwise.
327 .PP
328 \&\fIlh_error()\fR returns 1 if an error occurred in the last operation, 0
329 otherwise.
330 .PP
331 \&\fIlh_free()\fR, \fIlh_doall()\fR and \fIlh_doall_arg()\fR return no values.
332 .SH "NOTE"
333 .IX Header "NOTE"
334 The various \s-1LHASH\s0 macros and callback types exist to make it possible
335 to write type-safe code without resorting to function-prototype
336 casting \- an evil that makes application code much harder to
337 audit/verify and also opens the window of opportunity for stack
338 corruption and other hard-to-find bugs.  It also, apparently, violates
339 ANSI-C.
340 .PP
341 The \s-1LHASH\s0 code regards table entries as constant data.  As such, it
342 internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R"
343 pointer type.  This is why callbacks such as those used by \fIlh_doall()\fR
344 and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the
345 parameters that pass back the table items' data pointers \- for
346 consistency, user-provided data is \*(L"const\*(R" at all times as far as the
347 \&\s-1LHASH\s0 code is concerned.  However, as callers are themselves providing
348 these pointers, they can choose whether they too should be treating
349 all such parameters as constant.
350 .PP
351 As an example, a hash table may be maintained by code that, for
352 reasons of encapsulation, has only \*(L"const\*(R" access to the data being
353 indexed in the hash table (ie. it is returned as \*(L"const\*(R" from
354 elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are
355 appropriate as-is.  Conversely, if the caller is responsible for the
356 life-time of the data in question, then they may well wish to make
357 modifications to table item passed back in the \fIlh_doall()\fR or
358 \&\fIlh_doall_arg()\fR callbacks (see the \*(L"STUFF_cleanup\*(R" example above).  If
359 so, the caller can either cast the \*(L"const\*(R" away (if they're providing
360 the raw callbacks themselves) or use the macros to declare/implement
361 the wrapper functions without \*(L"const\*(R" types.
362 .PP
363 Callers that only have \*(L"const\*(R" access to data they're indexing in a
364 table, yet declare callbacks without constant types (or cast the
365 \&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs
366 without being encouraged to do so by the \s-1API. \s0 On a related note,
367 those auditing code should pay special attention to any instances of
368 DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types
369 without any \*(L"const\*(R" qualifiers.
370 .SH "BUGS"
371 .IX Header "BUGS"
372 \&\fIlh_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error.
373 .SH "INTERNALS"
374 .IX Header "INTERNALS"
375 The following description is based on the SSLeay documentation:
376 .PP
377 The \fBlhash\fR library implements a hash table described in the
378 \&\fICommunications of the \s-1ACM\s0\fR in 1991.  What makes this hash table
379 different is that as the table fills, the hash table is increased (or
380 decreased) in size via \fIOPENSSL_realloc()\fR.  When a 'resize' is done, instead of
381 all hashes being redistributed over twice as many 'buckets', one
382 bucket is split.  So when an 'expand' is done, there is only a minimal
383 cost to redistribute some values.  Subsequent inserts will cause more
384 single 'bucket' redistributions but there will never be a sudden large
385 cost due to redistributing all the 'buckets'.
386 .PP
387 The state for a particular hash table is kept in the \fB\s-1LHASH\s0\fR structure.
388 The decision to increase or decrease the hash table size is made
389 depending on the 'load' of the hash table.  The load is the number of
390 items in the hash table divided by the size of the hash table.  The
391 default values are as follows.  If (hash\->up_load < load) =>
392 expand.  if (hash\->down_load > load) => contract.  The
393 \&\fBup_load\fR has a default value of 1 and \fBdown_load\fR has a default value
394 of 2.  These numbers can be modified by the application by just
395 playing with the \fBup_load\fR and \fBdown_load\fR variables.  The 'load' is
396 kept in a form which is multiplied by 256.  So
397 hash\->up_load=8*256; will cause a load of 8 to be set.
398 .PP
399 If you are interested in performance the field to watch is
400 num_comp_calls.  The hash library keeps track of the 'hash' value for
401 each item so when a lookup is done, the 'hashes' are compared, if
402 there is a match, then a full compare is done, and
403 hash\->num_comp_calls is incremented.  If num_comp_calls is not equal
404 to num_delete plus num_retrieve it means that your hash function is
405 generating hashes that are the same for different values.  It is
406 probably worth changing your hash function if this is the case because
407 even if your hash table has 10 items in a 'bucket', it can be searched
408 with 10 \fBunsigned long\fR compares and 10 linked list traverses.  This
409 will be much less expensive that 10 calls to your compare function.
410 .PP
411 \&\fIlh_strhash()\fR is a demo string hashing function:
412 .PP
413 .Vb 1
414 \& unsigned long lh_strhash(const char *c);
415 .Ve
416 .PP
417 Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this
418 routine would not normally be passed to \fIlh_new()\fR, rather it would be
419 used in the function passed to \fIlh_new()\fR.
420 .SH "SEE ALSO"
421 .IX Header "SEE ALSO"
422 \&\fIlh_stats\fR\|(3)
423 .SH "HISTORY"
424 .IX Header "HISTORY"
425 The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL.
426 \&\fIlh_error()\fR was added in SSLeay 0.9.1b.
427 .PP
428 This manpage is derived from the SSLeay documentation.
429 .PP
430 In OpenSSL 0.9.7, all lhash functions that were passed function pointers
431 were changed for better type safety, and the function types \s-1LHASH_COMP_FN_TYPE,
432 LHASH_HASH_FN_TYPE, LHASH_DOALL_FN_TYPE\s0 and \s-1LHASH_DOALL_ARG_FN_TYPE \s0
433 became available.