5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 Pekka Riikonen
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.
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.
23 /****h* silccrypt/SILC Hash Interface
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).
34 /****s* silccrypt/SilcHashAPI/SilcHash
38 * typedef struct SilcHashStruct *SilcHash;
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
48 typedef struct SilcHashStruct *SilcHash;
50 /****s* silccrypt/SilcHashAPI/SilcHashObject
54 * typedef struct { ... } SilcHashObject;
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.
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)();
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)
82 /* Default hash functions for silc_hash_register_default(). */
83 extern DLLAPI const SilcHashObject silc_default_hash[];
85 /* Default HASH function in the SILC protocol */
86 #define SILC_DEFAULT_HASH "sha1"
90 /* Following macros are used to implement the SILC Hash API. These
91 macros should be used instead of declaring functions by hand. */
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"
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, \
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()
118 /****f* silccrypt/SilcHashAPI/silc_hash_register
122 * bool silc_hash_register(const SilcHashObject *hash);
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.
135 bool silc_hash_register(const SilcHashObject *hash);
137 /****f* silccrypt/SilcHashAPI/silc_hash_unregister
141 * bool silc_hash_unregister(SilcHashObject *hash);
145 * Unregister a hash function from SILC by the SilcHashObject `hash'.
146 * This should be called for all registered hash functions. Returns
150 bool silc_hash_unregister(SilcHashObject *hash);
152 /****f* silccrypt/SilcHashAPI/silc_hash_register_default
156 * bool silc_hash_register_default(void);
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).
169 bool silc_hash_register_default(void);
171 /****f* silccrypt/SilcHashAPI/silc_hash_alloc
175 * bool silc_hash_alloc(const unsigned char *name, SilcHash *new_hash);
179 * Allocates a new SilcHash object of name of `name'. The new allocated
180 * hash function is returned into `new_hash' pointer. This function
181 * returns FALSE if such hash function does not exist.
184 bool silc_hash_alloc(const unsigned char *name, SilcHash *new_hash);
186 /****f* silccrypt/SilcHashAPI/silc_hash_free
190 * void silc_hash_free(SilcHash hash);
194 * Frees the allocated hash function context.
197 void silc_hash_free(SilcHash hash);
199 /****f* silccrypt/SilcHashAPI/silc_hash_is_supported
203 * bool silc_hash_is_supported(const unsigned char *name);
207 * Returns TRUE if the hash function indicated by the `name' exists.
210 bool silc_hash_is_supported(const unsigned char *name);
212 /****f* silccrypt/SilcHashAPI/silc_hash_get_supported
216 * char *silc_hash_get_supported(void);
220 * Returns comma (`,') separated list of registered hash functions This
221 * is used for example when sending supported hash function list during
222 * the SILC Key Exchange protocol (SKE). The caller must free the returned
226 char *silc_hash_get_supported(void);
228 /****f* silccrypt/SilcHashAPI/silc_hash_len
232 * SilcUInt32 silc_hash_len(SilcHash hash);
236 * Returns the length of the message digest the hash function produce.
239 SilcUInt32 silc_hash_len(SilcHash hash);
241 /****f* silccrypt/SilcHashAPI/silc_hash_block_len
245 * SilcUInt32 silc_hash_block_len(SilcHash hash);
249 * Returns the block length of the hash function.
252 SilcUInt32 silc_hash_block_len(SilcHash hash);
254 /****f* silccrypt/SilcHashAPI/silc_hash_get_name
258 * const char *silc_hash_get_name(SilcHash hash);
262 * Returns the name of the hash function indicated by the `hash' context.
265 const char *silc_hash_get_name(SilcHash hash);
267 /****f* silccrypt/SilcHashAPI/silc_hash_make
271 * void silc_hash_make(SilcHash hash, const unsigned char *data,
272 * SilcUInt32 len, unsigned char *return_hash);
276 * Computes the message digest (hash) out of the data indicated by
277 * `data' of length of `len' bytes. Returns the message digest to the
278 * `return_hash' buffer which must be at least of the size of the
279 * message digest the `hash' produces.
282 void silc_hash_make(SilcHash hash, const unsigned char *data,
283 SilcUInt32 len, unsigned char *return_hash);
285 /****f* silccrypt/SilcHashAPI/silc_hash_init
289 * void silc_hash_init(SilcHash hash);
293 * Sometimes calling the silc_hash_make might not be the most optimal
294 * case of computing digests. If you have a lot of different data
295 * that you need to put together for computing a digest you may either
296 * put them into a buffer and compute the digest from the buffer by
297 * calling the silc_hash_make, or you can use the silc_hash_init,
298 * silc_hash_update and silc_hash_final to do the digest. This function
299 * prepares the allocated hash function context for this kind of digest
300 * computation. To add the data to be used in the digest computation
301 * call the silc_hash_update function.
304 void silc_hash_init(SilcHash hash);
306 /****f* silccrypt/SilcHashAPI/silc_hash_update
310 * void silc_hash_update(SilcHash hash, const unsigned char *data,
311 * SilcUInt32 data_len);
315 * This function may be called to add data to be used in the digest
316 * computation. This can be called multiple times to add data from
317 * many sources before actually computing the digest. Once you've
318 * added all the data you need you can call the silc_hash_final to
319 * actually produce the message digest value.
323 * unsigned char digest[20];
325 * silc_hash_init(hash);
326 * silc_hash_update(hash, data, data_len);
327 * silc_hash_update(hash, more_data, more_data_len);
328 * silc_hash_final(hash, digest);
331 void silc_hash_update(SilcHash hash, const unsigned char *data,
332 SilcUInt32 data_len);
334 /****f* silccrypt/SilcHashAPI/silc_hash_final
338 * void silc_hash_final(SilcHash hash, unsigned char *return_hash);
342 * This function is used to produce the final message digest from
343 * the data that has been added to the hash function context by calling
344 * the silc_hash_update function. The digest is copied in to the
345 * `return_hash' pointer which must be at least the size that
346 * the silc_hash_len returns.
349 void silc_hash_final(SilcHash hash, unsigned char *return_hash);
351 /****f* silccrypt/SilcHashAPI/silc_hash_transform
355 * void silc_hash_transform(SilcHash hash, SilcUInt32 *state,
356 * const unsigned char *data);
360 * This is special function for calling the hash function's internal
361 * digest generation function. The size of the `state' array and the
362 * sizeof the `data' buffer is hash function specific and must be
363 * known by the caller. Usually this function is not needed.
366 void silc_hash_transform(SilcHash hash, SilcUInt32 *state,
367 const unsigned char *data);
369 /****f* silccrypt/SilcHashAPI/silc_hash_fingerprint
373 * char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
374 * SilcUInt32 data_len);
378 * Utility function which can be used to create a textual fingerprint
379 * out of the data indicated by `data' of length of `data_len' bytes.
380 * If `hash' is NULL then SHA1 hash function is used automatically.
381 * The caller must free the returned string.
383 * Example output could be:
384 * 41BF 5C2E 4149 039A 3917 831F 65C4 0A69 F98B 0A4D
387 char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
388 SilcUInt32 data_len);
390 /****f* silccrypt/SilcHashAPI/silc_hash_babbleprint
394 * char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
395 * SilcUInt32 data_len);
399 * Utility function which can be used to create a textual babbleprint
400 * out of the data indicated by `data' of length of `data_len' bytes.
401 * If `hash' is NULL then SHA1 hash function is used automatically.
402 * The caller must free the returned string.
404 * The babbleprint is same as fingerprint but encoded in a form which
405 * makes it easier to pronounce. When verifying fingerprint for example
406 * over a phone call, the babbleprint makes it easier to read the
409 * Example output could be:
410 * xiber-zulad-vubug-noban-puvyc-labac-zonos-gedik-novem-rudog-tyxix
413 char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
414 SilcUInt32 data_len);