5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2008 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/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 /****d* silccrypt/Hashes
58 * Supported hash function names. These names can be given as argument
63 #define SILC_HASH_SHA256 "sha256" /* SHA-256 */
64 #define SILC_HASH_SHA512 "sha512" /* SHA-512 */
65 #define SILC_HASH_SHA1 "sha1" /* SHA-1 */
66 #define SILC_HASH_MD5 "md5" /* MD5 */
69 /* Hash implementation object */
77 void (*update)(void *, const unsigned char *, SilcUInt32);
78 void (*final)(void *, unsigned char *);
79 void (*transform)(void *, const unsigned char *);
80 SilcUInt32 (*context_len)();
83 /* Marks for all hash functions. This can be used in silc_hash_unregister
84 to unregister all hash function at once. */
85 #define SILC_ALL_HASH_FUNCTIONS ((SilcHashObject *)1)
87 /* Default hash functions for silc_hash_register_default(). */
88 extern DLLAPI const SilcHashObject silc_default_hash[];
91 #define SILC_HASH_MAXLEN 64
95 /* Following macros are used to implement the SILC Hash API. These
96 macros should be used instead of declaring functions by hand. */
98 /* Macros that can be used to declare SILC Hash API functions. */
99 #define SILC_HASH_API_INIT(hash) \
100 void silc_##hash##_init(void *context)
101 #define SILC_HASH_API_UPDATE(hash) \
102 void silc_##hash##_update(void *context, const unsigned char *data, \
104 #define SILC_HASH_API_FINAL(hash) \
105 void silc_##hash##_final(void *context, unsigned char *digest)
106 #define SILC_HASH_API_TRANSFORM(hash) \
107 void silc_##hash##_transform(void *state, const unsigned char *buffer)
108 #define SILC_HASH_API_CONTEXT_LEN(hash) \
109 SilcUInt32 silc_##hash##_context_len()
113 /****f* silccrypt/silc_hash_register
117 * SilcBool silc_hash_register(const SilcHashObject *hash);
121 * Registers a new hash function into the SILC. This function can be
122 * used at the initialization. All registered hash functions should be
123 * unregistered with silc_hash_unregister. Returns FALSE on error.
124 * Usually this function is not needed. The default hash functions are
125 * automatically registered. This can be used to change the order of
126 * the registered hash functions by re-registering them in desired order,
127 * or add new hash functions.
130 SilcBool silc_hash_register(const SilcHashObject *hash);
132 /****f* silccrypt/silc_hash_unregister
136 * SilcBool silc_hash_unregister(SilcHashObject *hash);
140 * Unregister a hash function from SILC by the SilcHashObject `hash'.
141 * This should be called for all hash functions registered with
142 * silc_hash_register. Returns FALSE on error.
145 SilcBool silc_hash_unregister(SilcHashObject *hash);
147 /****f* silccrypt/silc_hash_register_default
151 * SilcBool silc_hash_register_default(void);
155 * Registers all default hash functions into the SILC. Application
156 * need not call this directly. By calling silc_crypto_init this function
160 SilcBool silc_hash_register_default(void);
162 /****f* silccrypt/silc_hash_unregister_all
166 * SilcBool silc_hash_unregister_all(void);
170 * Unregisters all registered hash functions. Application need not
171 * call this directly. By calling silc_crypto_uninit this function is
175 SilcBool silc_hash_unregister_all(void);
177 /****f* silccrypt/silc_hash_alloc
181 * SilcBool silc_hash_alloc(const char *name, SilcHash *new_hash);
185 * Allocates a new SilcHash object of name of `name'. The new allocated
186 * hash function is returned into `new_hash' pointer. This function
187 * returns FALSE if such hash function does not exist.
190 SilcBool silc_hash_alloc(const char *name, SilcHash *new_hash);
192 /****f* silccrypt/silc_hash_alloc_by_oid
196 * SilcBool silc_hash_alloc_by_oid(const char *oid, SilcHash *new_hash);
200 * Same as silc_hash_alloc but allocates the hash algorithm by the
201 * hash algorithm OID string indicated by `oid'. Returns FALSE if such
202 * hash function does not exist.
205 SilcBool silc_hash_alloc_by_oid(const char *oid, SilcHash *new_hash);
207 /****f* silccrypt/silc_hash_free
211 * void silc_hash_free(SilcHash hash);
215 * Frees the allocated hash function context.
218 void silc_hash_free(SilcHash hash);
220 /****f* silccrypt/silc_hash_is_supported
224 * SilcBool silc_hash_is_supported(const char *name);
228 * Returns TRUE if the hash function indicated by the `name' exists.
231 SilcBool silc_hash_is_supported(const char *name);
233 /****f* silccrypt/silc_hash_get_supported
237 * char *silc_hash_get_supported(void);
241 * Returns comma (`,') separated list of registered hash functions This
242 * is used for example when sending supported hash function list during
243 * the SILC Key Exchange protocol (SKE). The caller must free the returned
247 char *silc_hash_get_supported(void);
249 /****f* silccrypt/silc_hash_len
253 * SilcUInt32 silc_hash_len(SilcHash hash);
257 * Returns the length of the message digest the hash function produce.
260 SilcUInt32 silc_hash_len(SilcHash hash);
262 /****f* silccrypt/silc_hash_block_len
266 * SilcUInt32 silc_hash_block_len(SilcHash hash);
270 * Returns the block length of the hash function.
273 SilcUInt32 silc_hash_block_len(SilcHash hash);
275 /****f* silccrypt/silc_hash_get_name
279 * const char *silc_hash_get_name(SilcHash hash);
283 * Returns the name of the hash function indicated by the `hash' context.
286 const char *silc_hash_get_name(SilcHash hash);
288 /****f* silccrypt/silc_hash_get_oid
292 * const char *silc_hash_get_name(SilcHash hash);
296 * Returns the hash OID string. Returns NULL if the hash doesn't have
297 * OID string. Use strlen() to get the OID string length.
300 const char *silc_hash_get_oid(SilcHash hash);
302 /****f* silccrypt/silc_hash_make
306 * void silc_hash_make(SilcHash hash, const unsigned char *data,
307 * SilcUInt32 len, unsigned char *return_hash);
311 * Computes the message digest (hash) out of the data indicated by
312 * `data' of length of `len' bytes. Returns the message digest to the
313 * `return_hash' buffer which must be at least of the size of the
314 * message digest the `hash' produces.
317 void silc_hash_make(SilcHash hash, const unsigned char *data,
318 SilcUInt32 len, unsigned char *return_hash);
320 /****f* silccrypt/silc_hash_init
324 * void silc_hash_init(SilcHash hash);
328 * Sometimes calling the silc_hash_make might not be the most optimal
329 * case of computing digests. If you have a lot of different data
330 * that you need to put together for computing a digest you may either
331 * put them into a buffer and compute the digest from the buffer by
332 * calling the silc_hash_make, or you can use the silc_hash_init,
333 * silc_hash_update and silc_hash_final to do the digest. This function
334 * prepares the allocated hash function context for this kind of digest
335 * computation. To add the data to be used in the digest computation
336 * call the silc_hash_update function.
339 void silc_hash_init(SilcHash hash);
341 /****f* silccrypt/silc_hash_update
345 * void silc_hash_update(SilcHash hash, const unsigned char *data,
346 * SilcUInt32 data_len);
350 * This function may be called to add data to be used in the digest
351 * computation. This can be called multiple times to add data from
352 * many sources before actually computing the digest. Once you've
353 * added all the data you need you can call the silc_hash_final to
354 * actually produce the message digest value.
358 * unsigned char digest[20];
360 * silc_hash_init(hash);
361 * silc_hash_update(hash, data, data_len);
362 * silc_hash_update(hash, more_data, more_data_len);
363 * silc_hash_final(hash, digest);
366 void silc_hash_update(SilcHash hash, const unsigned char *data,
367 SilcUInt32 data_len);
369 /****f* silccrypt/silc_hash_final
373 * void silc_hash_final(SilcHash hash, unsigned char *return_hash);
377 * This function is used to produce the final message digest from
378 * the data that has been added to the hash function context by calling
379 * the silc_hash_update function. The digest is copied in to the
380 * `return_hash' pointer which must be at least the size that
381 * the silc_hash_len returns.
384 void silc_hash_final(SilcHash hash, unsigned char *return_hash);
386 /****f* silccrypt/silc_hash_transform
390 * void silc_hash_transform(SilcHash hash, void *state,
391 * const unsigned char *data);
395 * This is special function for calling the hash function's internal
396 * digest generation function. The size of the `state' array and the
397 * sizeof the `data' buffer is hash function specific and must be
398 * known by the caller. Usually this function is not needed.
401 void silc_hash_transform(SilcHash hash, void *state,
402 const unsigned char *data);
404 /****f* silccrypt/silc_hash_fingerprint
408 * char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
409 * SilcUInt32 data_len);
413 * Utility function which can be used to create a textual fingerprint
414 * out of the data indicated by `data' of length of `data_len' bytes.
415 * If `hash' is NULL then SHA1 hash function is used automatically.
416 * The caller must free the returned string.
418 * Example output could be:
419 * 41BF 5C2E 4149 039A 3917 831F 65C4 0A69 F98B 0A4D
422 char *silc_hash_fingerprint(SilcHash hash, const unsigned char *data,
423 SilcUInt32 data_len);
425 /****f* silccrypt/silc_hash_babbleprint
429 * char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
430 * SilcUInt32 data_len);
434 * Utility function which can be used to create a textual babbleprint
435 * out of the data indicated by `data' of length of `data_len' bytes.
436 * If `hash' is NULL then SHA1 hash function is used automatically.
437 * The caller must free the returned string.
439 * The babbleprint is same as fingerprint but encoded in a form which
440 * makes it easier to pronounce. When verifying fingerprint for example
441 * over a phone call, the babbleprint makes it easier to read the
444 * Example output could be:
445 * xiber-zulad-vubug-noban-puvyc-labac-zonos-gedik-novem-rudog-tyxix
448 char *silc_hash_babbleprint(SilcHash hash, const unsigned char *data,
449 SilcUInt32 data_len);