Back to index

courier  0.68.2
hmac.h
Go to the documentation of this file.
00001 #ifndef       hmac_h
00002 #define       hmac_h
00003 
00004 #include      <string.h>
00005 
00006 /*
00007 ** Copyright 1998 - 2005 Double Precision, Inc.
00008 ** See COPYING for distribution information.
00009 */
00010 
00011 #ifdef __cplusplus
00012 extern "C" {
00013 #endif
00014 
00015 
00016 struct hmac_hashinfo {      /* HMAC hash function descriptor */
00017 
00018        const char *hh_name; /* Name of this hash function (md5, sha1...) */
00019 
00020        size_t hh_B;  /* Length of compression blocks */
00021        size_t hh_L;  /* Length of hash outputs */
00022 
00023        size_t hh_S;  /* Length of 'context structure' */
00024 
00025        /* Hash functions */
00026 
00027        void (*hh_init)(void *);    /* Initialize context structure */
00028        void (*hh_hash)(void *, const void *, unsigned);
00029                                    /* Feed the hash function */
00030        void (*hh_endhash)(void *, unsigned long); /* Calculate final hash */
00031 
00032        void (*hh_getdigest)(void *, unsigned char *); /* Get the hash value */
00033        void (*hh_setdigest)(void *, const unsigned char *);
00034                                           /* Set the hash value */
00035 
00036        /* Some helper functions */
00037 
00038        /* Allocate context on stack, instead of mallocing it.  Calls the
00039        ** provided function pointer, with context as first arg.  The second
00040        ** arg will be passed as provided.
00041        */
00042 
00043        void (*hh_allocacontext)(void (*)(void *, void *), void *);
00044 
00045        /* Like allocacontext, but alloc buffer for hash value, hh_L */
00046 
00047        void (*hh_allocaval)(void (*)(unsigned char *, void *), void *);
00048 
00049        } ;
00050 
00051 /* Known hash functions */
00052 
00053 extern struct hmac_hashinfo hmac_md5, hmac_sha1, hmac_sha256;
00054 
00055 /*
00056 ** List of installed hash functions, dynamically generated at configuration
00057 ** time.
00058 */
00059 
00060 extern struct hmac_hashinfo *hmac_list[];
00061 
00062 /*
00063    To calculate an HMAC, allocate three buffers - outer, inner, and hash.
00064    Call hmac_hashkey, then hmac_hashtext.
00065 
00066    After hmac_haskey returns, the contents of inner and outer can be
00067    saved, as they contain a complete intermediate state of the hash
00068    calculation.
00069 */
00070 
00071 void hmac_hashkey(
00072        const struct hmac_hashinfo *,
00073        const char *, /* Key */
00074        size_t,       /* Key length */
00075        unsigned char *,     /* Output - outer buffer, prehashed */
00076        unsigned char *);    /* Output - inner buffer, prehashed */
00077 
00078 void hmac_hashtext (
00079        const struct hmac_hashinfo *,
00080        const char *, /* Text */
00081        size_t,       /* Text length */
00082        const unsigned char *,      /* outer buffer, prehashed */
00083        const unsigned char *,      /* inner buffer, prehashed */
00084        unsigned char *);    /* Output - the hash */
00085 
00086 #ifdef __cplusplus
00087 }
00088 #endif
00089 
00090 #endif