X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/20aa4704667efe38520849bd9c32040a9958bc9a..07290a455e008c37adc233554eff8ad468608161:/ghash.h diff --git a/ghash.h b/ghash.h index 725a0a3..d20aff4 100644 --- a/ghash.h +++ b/ghash.h @@ -1,13 +1,13 @@ /* -*-c-*- * - * $Id: ghash.h,v 1.4 2000/07/03 18:08:24 mdw Exp $ + * $Id$ * * Generic hash function interface * * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Catacomb. * @@ -15,38 +15,18 @@ * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * + * * Catacomb is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public * License along with Catacomb; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: ghash.h,v $ - * Revision 1.4 2000/07/03 18:08:24 mdw - * Include `bits.h'. - * - * Revision 1.3 2000/07/02 18:27:42 mdw - * (ghash->ops->done): Interface change. Passing in a null buffer pointer - * uses a buffer internal to the ghash object. The operation returns the - * address of the buffer it used. Clients of generic hashes no longer need - * to use dynamically allocated memory for hash results. - * - * Revision 1.2 2000/06/17 11:22:17 mdw - * Minor changes in the generic hash interface. - * - * Revision 1.1 1999/12/10 23:16:01 mdw - * Generic interface. - * - */ - #ifndef CATACOMB_GHASH_H #define CATACOMB_GHASH_H @@ -71,14 +51,102 @@ typedef struct ghash_ops { void (*hash)(ghash */*h*/, const void */*p*/, size_t /*sz*/); /* Hash */ octet *(*done)(ghash */*h*/, void */*buf*/); /* Write result */ void (*destroy)(ghash */*h*/); /* Destroy hash block */ + ghash *(*copy)(ghash */*h*/); /* Make a copy of the hash context */ } ghash_ops; +#define GH_INIT(ch) (ch)->init() +#define GH_CLASS(h) (h)->ops->c +#define GH_HASH(h, p, sz) (h)->ops->hash((h), (p), (sz)) +#define GH_DONE(h, buf) (h)->ops->done((h), (buf)) +#define GH_DESTROY(h) (h)->ops->destroy((h)) +#define GH_COPY(h) (h)->ops->copy((h)) + +#define GH_HASHU_(h, n, W) do { \ + TY_U##W n_ = (n); octet b_[SZ_##W]; \ + STORE##W(b_, n_); GH_HASH((h), b_, SZ_##W); \ +} while (0) +#define GH_HASHU8(h, n) GH_HASHU_((h), (n), 8) +#define GH_HASHU16(h, n) GH_HASHU_((h), (n), 16) +#define GH_HASHU16_B(h, n) GH_HASHU_((h), (n), 16_B) +#define GH_HASHU16_L(h, n) GH_HASHU_((h), (n), 16_L) +#define GH_HASHU24(h, n) GH_HASHU_((h), (n), 24) +#define GH_HASHU24_B(h, n) GH_HASHU_((h), (n), 24_B) +#define GH_HASHU24_L(h, n) GH_HASHU_((h), (n), 24_L) +#define GH_HASHU32(h, n) GH_HASHU_((h), (n), 32) +#define GH_HASHU32_B(h, n) GH_HASHU_((h), (n), 32_B) +#define GH_HASHU32_L(h, n) GH_HASHU_((h), (n), 32_L) +#ifdef HAVE_UINT64 +# define GH_HASHU64(h, n) GH_HASHU_((h), (n), 64) +# define GH_HASHU64_B(h, n) GH_HASHU_((h), (n), 64_B) +# define GH_HASHU64_L(h, n) GH_HASHU_((h), (n), 64_L) +#endif + +#define GH_HASHBUF_(h, p, sz, W) do { \ + size_t sz_ = (sz); assert(sz_ <= MASK##W); \ + GH_HASHU_(h, sz_, W); GH_HASH(h, (p), sz_); \ +} while (0) +#define GH_HASHBUF8(h, p, sz) GH_HASHBUF_((h), (p), (sz), 8) +#define GH_HASHBUF16(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16) +#define GH_HASHBUF16_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_L) +#define GH_HASHBUF16_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_B) +#define GH_HASHBUF24(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24) +#define GH_HASHBUF24_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_L) +#define GH_HASHBUF24_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_B) +#define GH_HASHBUF32(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32) +#define GH_HASHBUF32_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_L) +#define GH_HASHBUF32_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_B) +#ifdef HAVE_UINT64 +# define GH_HASHBUF64(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64) +# define GH_HASHBUF64_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_L) +# define GH_HASHBUF64_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_B) +#endif + +#define GH_HASHSTR_(h, p, W) do { \ + const char *p_ = (p); GH_HASHBUF_((h), p_, strlen(p_), W); \ +} while (0) +#define GH_HASHSTR8(h, p) GH_HASHSTR_((h), (p), 8) +#define GH_HASHSTR16(h, p) GH_HASHSTR_((h), (p), 16) +#define GH_HASHSTR16_L(h, p) GH_HASHSTR_((h), (p), 16_L) +#define GH_HASHSTR16_B(h, p) GH_HASHSTR_((h), (p), 16_B) +#define GH_HASHSTR24(h, p) GH_HASHSTR_((h), (p), 24) +#define GH_HASHSTR24_L(h, p) GH_HASHSTR_((h), (p), 24_L) +#define GH_HASHSTR24_B(h, p) GH_HASHSTR_((h), (p), 24_B) +#define GH_HASHSTR32(h, p) GH_HASHSTR_((h), (p), 32) +#define GH_HASHSTR32_L(h, p) GH_HASHSTR_((h), (p), 32_L) +#define GH_HASHSTR32_B(h, p) GH_HASHSTR_((h), (p), 32_B) +#ifdef HAVE_UINT64 +# define GH_HASHSTR64(h, p) GH_HASHSTR_((h), (p), 64) +# define GH_HASHSTR64_L(h, p) GH_HASHSTR_((h), (p), 64_L) +# define GH_HASHSTR64_B(h, p) GH_HASHSTR_((h), (p), 64_B) +#endif + +#define GH_HASHSTRZ(h, p) do { \ + const char *p_ = (p); GH_HASH((h), p_, strlen(p_) + 1); \ +} while (0) +#define GH_HASHSTR(h, p) do { \ + const char *p_ = (p); GH_HASH((h), p_, strlen(p_)); \ +} while (0) + typedef struct gchash { const char *name; /* Name of the hash function */ size_t hashsz; /* Size of output hash */ ghash *(*init)(void); /* Create a new hash instance */ + size_t bufsz; /* Buffer size, or zero */ } gchash; +/*----- Tables ------------------------------------------------------------*/ + +extern const gchash *const ghashtab[]; + +/* --- @ghash_byname@ --- * + * + * Arguments: @const char *p@ = pointer to name string + * + * Returns: The named cipher class, or null. + */ + +extern const gchash *ghash_byname(const char */*p*/); + /*----- That's all, folks -------------------------------------------------*/ #ifdef __cplusplus