| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: ghash-def.h,v 1.4 2000/07/15 10:00:58 mdw Exp $ |
| 4 | * |
| 5 | * Definitions for generic hash interface |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: ghash-def.h,v $ |
| 33 | * Revision 1.4 2000/07/15 10:00:58 mdw |
| 34 | * New generic hash operation for copying hash contexts. |
| 35 | * |
| 36 | * Revision 1.3 2000/07/02 18:27:42 mdw |
| 37 | * (ghash->ops->done): Interface change. Passing in a null buffer pointer |
| 38 | * uses a buffer internal to the ghash object. The operation returns the |
| 39 | * address of the buffer it used. Clients of generic hashes no longer need |
| 40 | * to use dynamically allocated memory for hash results. |
| 41 | * |
| 42 | * Revision 1.2 2000/06/17 11:22:03 mdw |
| 43 | * Use secure arena for memory allocation. Minor changes in the generic |
| 44 | * hash interface. |
| 45 | * |
| 46 | * Revision 1.1 1999/12/10 23:21:37 mdw |
| 47 | * Generic interface. |
| 48 | * |
| 49 | */ |
| 50 | |
| 51 | #ifndef CATACOMB_GHASH_DEF_H |
| 52 | #define CATACOMB_GHASH_DEF_H |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | /*----- Header files ------------------------------------------------------*/ |
| 59 | |
| 60 | #include <mLib/bits.h> |
| 61 | #include <mLib/sub.h> |
| 62 | |
| 63 | #ifndef CATACOMB_ARENA_H |
| 64 | # include "arena.h" |
| 65 | #endif |
| 66 | |
| 67 | #ifndef CATACOMB_GHASH_H |
| 68 | # include "ghash.h" |
| 69 | #endif |
| 70 | |
| 71 | /*----- Generic hash function interface -----------------------------------*/ |
| 72 | |
| 73 | /* --- @GHASH_DEF@ --- * |
| 74 | * |
| 75 | * Arguments: @PRE, pre@ = prefixes for hash function |
| 76 | * |
| 77 | * Use: Defines the generic hash instance. |
| 78 | */ |
| 79 | |
| 80 | #define GHASH_DEF(PRE, pre) \ |
| 81 | \ |
| 82 | static const ghash_ops gops; \ |
| 83 | \ |
| 84 | typedef struct gctx { \ |
| 85 | ghash h; \ |
| 86 | pre##_ctx c; \ |
| 87 | octet buf[PRE##_HASHSZ]; \ |
| 88 | } gctx; \ |
| 89 | \ |
| 90 | static ghash *ghinit(void) \ |
| 91 | { \ |
| 92 | gctx *g = S_CREATE(gctx); \ |
| 93 | g->h.ops = &gops; \ |
| 94 | pre##_init(&g->c); \ |
| 95 | return (&g->h); \ |
| 96 | } \ |
| 97 | \ |
| 98 | static void ghhash(ghash *h, const void *p, size_t sz) \ |
| 99 | { \ |
| 100 | gctx *g = (gctx *)h; \ |
| 101 | pre##_hash(&g->c, p, sz); \ |
| 102 | } \ |
| 103 | \ |
| 104 | static octet *ghdone(ghash *h, void *buf) \ |
| 105 | { \ |
| 106 | gctx *g = (gctx *)h; \ |
| 107 | if (!buf) \ |
| 108 | buf = g->buf; \ |
| 109 | pre##_done(&g->c, buf); \ |
| 110 | return (buf); \ |
| 111 | } \ |
| 112 | \ |
| 113 | static void ghdestroy(ghash *h) \ |
| 114 | { \ |
| 115 | gctx *g = (gctx *)h; \ |
| 116 | S_DESTROY(g); \ |
| 117 | } \ |
| 118 | \ |
| 119 | static ghash *ghcopy(ghash *h) \ |
| 120 | { \ |
| 121 | gctx *g = (gctx *)h; \ |
| 122 | gctx *gg = S_CREATE(gctx); \ |
| 123 | memcpy(gg, g, sizeof(gctx)); \ |
| 124 | return (&gg->h); \ |
| 125 | } \ |
| 126 | \ |
| 127 | static const ghash_ops gops = \ |
| 128 | { &pre, ghhash, ghdone, ghdestroy, ghcopy }; \ |
| 129 | const gchash pre = { #pre, PRE##_HASHSZ, ghinit }; |
| 130 | |
| 131 | /*----- That's all, folks -------------------------------------------------*/ |
| 132 | |
| 133 | #ifdef __cplusplus |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #endif |