206427ebe4892bd857a3c30f6f9e0588510f0f7d
[silc.git] / lib / silccrypt / silchash.h
1 /*
2
3   silchash.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef SILCHASH_H
21 #define SILCHASH_H
22
23 /****h* silccrypt/SILC Hash Interface
24  *
25  * DESCRIPTION
26  *
27  *    This is the interface for hash functions which are used to create
28  *    message digests.  The routines are used in various cryptographic
29  *    operations.  SILC Hash Interface is used for example by the
30  *    SILC HMAC Interface (SilcHmac).
31  *
32  ***/
33
34 /****s* silccrypt/SilcHashAPI/SilcHash
35  *
36  * NAME
37  * 
38  *    typedef struct SilcHashStruct *SilcHash;
39  *
40  * DESCRIPTION
41  *
42  *    This context is the actual hash function context and is allocated
43  *    by silc_hash_alloc and given as argument usually to all
44  *    silc_hash_* functions.  It is freed by the silc_hash_free
45  *    function.
46  *
47  ***/
48 typedef struct SilcHashStruct *SilcHash;
49
50 /****s* silccrypt/SilcHashAPI/SilcHashObject
51  *
52  * NAME
53  * 
54  *    typedef struct { ... } SilcHashObject;
55  *
56  * DESCRIPTION
57  *
58  *    This structure represents one hash function.  The hash function's
59  *    name, digest length and block length are defined in the structure.
60  *    This structure is then given as argument to the silc_hash_register.
61  *    That function is used to register all hash functions into SILC.
62  *    They can be then allocated by the name found in this structure by
63  *    calling the silc_hash_alloc.
64  *
65  ***/
66 typedef struct {
67   char *name;
68   SilcUInt32 hash_len;
69   SilcUInt32 block_len;
70
71   void (*init)(void *);
72   void (*update)(void *, const unsigned char *, SilcUInt32);
73   void (*final)(void *, unsigned char *);
74   void (*transform)(SilcUInt32 *, const unsigned char *);
75   SilcUInt32 (*context_len)();
76 } SilcHashObject;
77
78 /* Marks for all hash functions. This can be used in silc_hash_unregister
79    to unregister all hash function at once. */
80 #define SILC_ALL_HASH_FUNCTIONS ((SilcHashObject *)1)
81
82 /* Default hash functions for silc_hash_register_default(). */
83 extern DLLAPI const SilcHashObject silc_default_hash[];
84
85 /* Default HASH function in the SILC protocol */
86 #define SILC_DEFAULT_HASH "sha1"
87
88 /* Macros */
89
90 /* Following macros are used to implement the SILC Hash API. These
91    macros should be used instead of declaring functions by hand. */
92
93 /* Function names in SILC Hash modules. The name of the hash function
94    is appended into these names and used to the get correct symbol out
95    of the module. All SILC Hash API compliant modules has to support
96    these names as function names (use macros below to assure this). */
97 #define SILC_HASH_SIM_INIT "init"
98 #define SILC_HASH_SIM_UPDATE "update"
99 #define SILC_HASH_SIM_FINAL "final"
100 #define SILC_HASH_SIM_TRANSFORM "transform"
101 #define SILC_HASH_SIM_CONTEXT_LEN "context_len"
102
103 /* Macros that can be used to declare SILC Hash API functions. */
104 #define SILC_HASH_API_INIT(hash)                                        \
105 void silc_##hash##_init(void *context)
106 #define SILC_HASH_API_UPDATE(hash)                                      \
107 void silc_##hash##_update(void *context, const unsigned char *data,     \
108                           SilcUInt32 len)
109 #define SILC_HASH_API_FINAL(hash)                                       \
110 void silc_##hash##_final(void *context, unsigned char *digest)
111 #define SILC_HASH_API_TRANSFORM(hash)                                   \
112 void silc_##hash##_transform(SilcUInt32 *state, const unsigned char *buffer)
113 #define SILC_HASH_API_CONTEXT_LEN(hash)                                 \
114 SilcUInt32 silc_##hash##_context_len()
115
116 /* Prototypes */
117
118 /****f* silccrypt/SilcHashAPI/silc_hash_register
119  *
120  * SYNOPSIS
121  *
122  *    bool silc_hash_register(const SilcHashObject *hash);
123  *
124  * DESCRIPTION
125  *
126  *    Registers a new hash function into the SILC.  This function is used
127  *    at the initialization of the SILC.  All registered hash functions
128  *    should be unregistered with silc_hash_unregister.  The `hash' includes
129  *    the name of the hash function, digest length and block length.  Usually
130  *    this function is not called directly.  Instead, application can call
131  *    the silc_hash_register_default to register all default hash functions
132  *    that are builtin the sources.  Returns FALSE on error.
133  *
134  ***/
135 bool silc_hash_register(const SilcHashObject *hash);
136
137 /****f* silccrypt/SilcHashAPI/silc_hash_unregister
138  *
139  * SYNOPSIS
140  *
141  *    bool silc_hash_unregister(SilcHashObject *hash);
142  *
143  * DESCRIPTION
144  *
145  *    Unregister a hash function from SILC by the SilcHashObject `hash'.
146  *    This should be called for all registered hash functions.  Returns
147  *    FALSE on error.
148  *
149  ***/
150 bool silc_hash_unregister(SilcHashObject *hash);
151
152 /****f* silccrypt/SilcHashAPI/silc_hash_register_default
153  *
154  * SYNOPSIS
155  *
156  *    bool silc_hash_register_default(void);
157  *
158  * DESCRIPTION
159  *
160  *    Registers all default hash functions into the SILC.  These are the
161  *    hash functions that are builtin in the sources.  See the list of
162  *    default hash functions in the silchash.c source file.  The application
163  *    may use this to register default hash functions if specific hash
164  *    function in any specific order is not wanted (application's
165  *    configuration usually may decide the order of the registration, in
166  *    which case this function should not be used).
167  *
168  ***/
169 bool silc_hash_register_default(void);
170
171 /****f* silccrypt/SilcHashAPI/silc_hash_unregister_all
172  *
173  * SYNOPSIS
174  *
175  *    bool silc_hash_unregister_all(void);
176  *
177  * DESCRIPTION
178  *
179  *    Unregisters all registered hash functions.
180  *
181  ***/
182 bool silc_hash_unregister_all(void);
183
184 /****f* silccrypt/SilcHashAPI/silc_hash_alloc
185  *
186  * SYNOPSIS
187  *
188  *    bool silc_hash_alloc(const unsigned char *name, SilcHash *new_hash);
189  *
190  * DESCRIPTION
191  *
192  *    Allocates a new SilcHash object of name of `name'.  The new allocated
193  *    hash function is returned into `new_hash' pointer.  This function
194  *    returns FALSE if such hash function does not exist.
195  *
196  ***/
197 bool silc_hash_alloc(const unsigned char *name, SilcHash *new_hash);
198
199 /****f* silccrypt/SilcHashAPI/silc_hash_free
200  *
201  * SYNOPSIS
202  *
203  *    void silc_hash_free(SilcHash hash);
204  *
205  * DESCRIPTION
206  *
207  *    Frees the allocated hash function context.
208  *
209  ***/
210 void silc_hash_free(SilcHash hash);
211
212 /****f* silccrypt/SilcHashAPI/silc_hash_is_supported
213  *
214  * SYNOPSIS
215  *
216  *    bool silc_hash_is_supported(const unsigned char *name);
217  *
218  * DESCRIPTION
219  *
220  *    Returns TRUE if the hash function indicated by the `name' exists.
221  *
222  ***/
223 bool silc_hash_is_supported(const unsigned char *name);
224
225 /****f* silccrypt/SilcHashAPI/silc_hash_get_supported
226  *
227  * SYNOPSIS
228  *
229  *    char *silc_hash_get_supported(void);
230  *
231  * DESCRIPTION
232  *
233  *    Returns comma (`,') separated list of registered hash functions  This
234  *    is used for example when sending supported hash function list during
235  *    the SILC Key Exchange protocol (SKE).  The caller must free the returned
236  *    pointer.
237  *
238  ***/
239 char *silc_hash_get_supported(void);
240
241 /****f* silccrypt/SilcHashAPI/silc_hash_len
242  *
243  * SYNOPSIS
244  *
245  *    SilcUInt32 silc_hash_len(SilcHash hash);
246  *
247  * DESCRIPTION
248  *
249  *    Returns the length of the message digest the hash function produce.
250  *
251  ***/
252 SilcUInt32 silc_hash_len(SilcHash hash);
253
254 /****f* silccrypt/SilcHashAPI/silc_hash_block_len
255  *
256  * SYNOPSIS
257  *
258  *    SilcUInt32 silc_hash_block_len(SilcHash hash);
259  *
260  * DESCRIPTION
261  *
262  *    Returns the block length of the hash function.
263  *
264  ***/
265 SilcUInt32 silc_hash_block_len(SilcHash hash);
266
267 /****f* silccrypt/SilcHashAPI/silc_hash_get_name
268  *
269  * SYNOPSIS
270  *
271  *    const char *silc_hash_get_name(SilcHash hash);
272  *
273  * DESCRIPTION
274  *
275  *    Returns the name of the hash function indicated by the `hash' context.
276  *
277  ***/
278 const char *silc_hash_get_name(SilcHash hash);
279
280 /****f* silccrypt/SilcHashAPI/silc_hash_make
281  *
282  * SYNOPSIS
283  *
284  *    void silc_hash_make(SilcHash hash, const unsigned char *data,
285  *                        SilcUInt32 len, unsigned char *return_hash);
286  *
287  * DESCRIPTION
288  *
289  *    Computes the message digest (hash) out of the data indicated by
290  *    `data' of length of `len' bytes.  Returns the message digest to the
291  *    `return_hash' buffer which must be at least of the size of the
292  *    message digest the `hash' produces.
293  *
294  ***/
295 void silc_hash_make(SilcHash hash, const unsigned char *data,
296                     SilcUInt32 len, unsigned char *return_hash);
297
298 /****f* silccrypt/SilcHashAPI/silc_hash_init
299  *
300  * SYNOPSIS
301  *
302  *    void silc_hash_init(SilcHash hash);
303  *
304  * DESCRIPTION
305  *
306  *    Sometimes calling the silc_hash_make might not be the most optimal
307  *    case of computing digests.  If you have a lot of different data
308  *    that you need to put together for computing a digest you may either
309  *    put them into a buffer and compute the digest from the buffer by
310  *    calling the silc_hash_make, or you can use the silc_hash_init,
311  *    silc_hash_update and silc_hash_final to do the digest.  This function
312  *    prepares the allocated hash function context for this kind of digest 
313  *    computation.  To add the data to be used in the digest computation
314  *    call the silc_hash_update function.
315  *
316  ***/
317 void silc_hash_init(SilcHash hash);
318
319 /****f* silccrypt/SilcHashAPI/silc_hash_update
320  *
321  * SYNOPSIS
322  *
323  *    void silc_hash_update(SilcHash hash, const unsigned char *data,
324  *                          SilcUInt32 data_len);
325  *
326  * DESCRIPTION
327  *
328  *    This function may be called to add data to be used in the digest
329  *    computation.  This can be called multiple times to add data from
330  *    many sources before actually computing the digest.  Once you've
331  *    added all the data you need you can call the silc_hash_final to
332  *    actually produce the message digest value.
333  *
334  * EXAMPLE
335  *
336  *    unsigned char digest[20];
337  *
338  *    silc_hash_init(hash);
339  *    silc_hash_update(hash, data, data_len);
340  *    silc_hash_update(hash, more_data, more_data_len);
341  *    silc_hash_final(hash, digest);
342  *
343  ***/
344 void silc_hash_update(SilcHash hash, const unsigned char *data,
345                       SilcUInt32 data_len);
346
347 /****f* silccrypt/SilcHashAPI/silc_hash_final
348  *
349  * SYNOPSIS
350  *
351  *    void silc_hash_final(SilcHash hash, unsigned char *return_hash);
352  *
353  * DESCRIPTION
354  *
355  *    This function is used to produce the final message digest from
356  *    the data that has been added to the hash function context by calling
357  *    the silc_hash_update function.  The digest is copied in to the
358  *    `return_hash' pointer which must be at least the size that
359  *    the silc_hash_len returns.
360  *
361  ***/
362 void silc_hash_final(SilcHash hash, unsigned char *return_hash);
363
364 /****f* silccrypt/SilcHashAPI/silc_hash_transform
365  *
366  * SYNOPSIS
367  *
368  *    void silc_hash_transform(SilcHash hash, SilcUInt32 *state,
369  *                             const unsigned char *data);
370  *
371  * DESCRIPTION
372  *
373  *    This is special function for calling the hash function's internal
374  *    digest generation function.  The size of the `state' array and the
375  *    sizeof the `data' buffer is hash function specific and must be
376  *    known by the caller.  Usually this function is not needed.
377  *
378  ***/
379 void silc_hash_transform(SilcHash hash, SilcUInt32 *state,
380                          const unsigned char *data);
381
382 /****f* silccrypt/SilcHashAPI/silc_hash_fingerprint
383  *
384  * SYNOPSIS
385  *
386  *    char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
387  *                                SilcUInt32 data_len);
388  *
389  * DESCRIPTION
390  *
391  *    Utility function which can be used to create a textual fingerprint
392  *    out of the data indicated by `data' of length of `data_len' bytes.
393  *    If `hash' is NULL then SHA1 hash function is used automatically.
394  *    The caller must free the returned string.
395  *
396  *    Example output could be:
397  *      41BF 5C2E 4149 039A 3917  831F 65C4 0A69 F98B 0A4D
398  *
399  ***/
400 char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
401                             SilcUInt32 data_len);
402
403 /****f* silccrypt/SilcHashAPI/silc_hash_babbleprint
404  *
405  * SYNOPSIS
406  *
407  *    char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
408  *                                SilcUInt32 data_len);
409  *
410  * DESCRIPTION
411  *
412  *    Utility function which can be used to create a textual babbleprint
413  *    out of the data indicated by `data' of length of `data_len' bytes.
414  *    If `hash' is NULL then SHA1 hash function is used automatically.
415  *    The caller must free the returned string.
416  *
417  *    The babbleprint is same as fingerprint but encoded in a form which
418  *    makes it easier to pronounce.  When verifying fingerprint for example
419  *    over a phone call, the babbleprint makes it easier to read the
420  *    fingerprint.
421  *
422  *    Example output could be:
423  *      xiber-zulad-vubug-noban-puvyc-labac-zonos-gedik-novem-rudog-tyxix
424  *
425  ***/
426 char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
427                             SilcUInt32 data_len);
428
429 #endif