| 1 | #ifndef hmac_h |
| 2 | #define hmac_h |
| 3 | |
| 4 | #include <string.h> |
| 5 | |
| 6 | /* |
| 7 | ** Copyright 1998 - 2005 Double Precision, Inc. |
| 8 | ** See COPYING for distribution information. |
| 9 | */ |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | static const char hmac_h_rcsid[]="$Id: hmac.h,v 1.6 2005/02/21 03:18:30 mrsam Exp $"; |
| 16 | |
| 17 | struct hmac_hashinfo { /* HMAC hash function descriptor */ |
| 18 | |
| 19 | const char *hh_name; /* Name of this hash function (md5, sha1...) */ |
| 20 | |
| 21 | size_t hh_B; /* Length of compression blocks */ |
| 22 | size_t hh_L; /* Length of hash outputs */ |
| 23 | |
| 24 | size_t hh_S; /* Length of 'context structure' */ |
| 25 | |
| 26 | /* Hash functions */ |
| 27 | |
| 28 | void (*hh_init)(void *); /* Initialize context structure */ |
| 29 | void (*hh_hash)(void *, const void *, unsigned); |
| 30 | /* Feed the hash function */ |
| 31 | void (*hh_endhash)(void *, unsigned long); /* Calculate final hash */ |
| 32 | |
| 33 | void (*hh_getdigest)(void *, unsigned char *); /* Get the hash value */ |
| 34 | void (*hh_setdigest)(void *, const unsigned char *); |
| 35 | /* Set the hash value */ |
| 36 | |
| 37 | /* Some helper functions */ |
| 38 | |
| 39 | /* Allocate context on stack, instead of mallocing it. Calls the |
| 40 | ** provided function pointer, with context as first arg. The second |
| 41 | ** arg will be passed as provided. |
| 42 | */ |
| 43 | |
| 44 | void (*hh_allocacontext)(void (*)(void *, void *), void *); |
| 45 | |
| 46 | /* Like allocacontext, but alloc buffer for hash value, hh_L */ |
| 47 | |
| 48 | void (*hh_allocaval)(void (*)(unsigned char *, void *), void *); |
| 49 | |
| 50 | } ; |
| 51 | |
| 52 | /* Known hash functions */ |
| 53 | |
| 54 | extern struct hmac_hashinfo hmac_md5, hmac_sha1, hmac_sha256; |
| 55 | |
| 56 | /* |
| 57 | ** List of installed hash functions, dynamically generated at configuration |
| 58 | ** time. |
| 59 | */ |
| 60 | |
| 61 | extern struct hmac_hashinfo *hmac_list[]; |
| 62 | |
| 63 | /* |
| 64 | To calculate an HMAC, allocate three buffers - outer, inner, and hash. |
| 65 | Call hmac_hashkey, then hmac_hashtext. |
| 66 | |
| 67 | After hmac_haskey returns, the contents of inner and outer can be |
| 68 | saved, as they contain a complete intermediate state of the hash |
| 69 | calculation. |
| 70 | */ |
| 71 | |
| 72 | void hmac_hashkey( |
| 73 | const struct hmac_hashinfo *, |
| 74 | const char *, /* Key */ |
| 75 | size_t, /* Key length */ |
| 76 | unsigned char *, /* Output - outer buffer, prehashed */ |
| 77 | unsigned char *); /* Output - inner buffer, prehashed */ |
| 78 | |
| 79 | void hmac_hashtext ( |
| 80 | const struct hmac_hashinfo *, |
| 81 | const char *, /* Text */ |
| 82 | size_t, /* Text length */ |
| 83 | const unsigned char *, /* outer buffer, prehashed */ |
| 84 | const unsigned char *, /* inner buffer, prehashed */ |
| 85 | unsigned char *); /* Output - the hash */ |
| 86 | |
| 87 | #ifdef __cplusplus |
| 88 | } ; |
| 89 | #endif |
| 90 | |
| 91 | #endif |