Fix bit operations. Test them (a bit) better.
[u/mdw/catacomb] / ghash-def.h
CommitLineData
a4738812 1/* -*-c-*-
2 *
f80463ac 3 * $Id: ghash-def.h,v 1.5 2001/01/25 21:39:58 mdw Exp $
a4738812 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 $
f80463ac 33 * Revision 1.5 2001/01/25 21:39:58 mdw
34 * Burn the hash context when it's done with, for paranoia's sake.
35 *
d2fdbc2c 36 * Revision 1.4 2000/07/15 10:00:58 mdw
37 * New generic hash operation for copying hash contexts.
38 *
a351d052 39 * Revision 1.3 2000/07/02 18:27:42 mdw
40 * (ghash->ops->done): Interface change. Passing in a null buffer pointer
41 * uses a buffer internal to the ghash object. The operation returns the
42 * address of the buffer it used. Clients of generic hashes no longer need
43 * to use dynamically allocated memory for hash results.
44 *
28ef1f45 45 * Revision 1.2 2000/06/17 11:22:03 mdw
46 * Use secure arena for memory allocation. Minor changes in the generic
47 * hash interface.
48 *
a4738812 49 * Revision 1.1 1999/12/10 23:21:37 mdw
50 * Generic interface.
51 *
52 */
53
54#ifndef CATACOMB_GHASH_DEF_H
55#define CATACOMB_GHASH_DEF_H
56
57#ifdef __cplusplus
58 extern "C" {
59#endif
60
61/*----- Header files ------------------------------------------------------*/
62
63#include <mLib/bits.h>
64#include <mLib/sub.h>
65
28ef1f45 66#ifndef CATACOMB_ARENA_H
67# include "arena.h"
68#endif
69
a4738812 70#ifndef CATACOMB_GHASH_H
71# include "ghash.h"
72#endif
73
f80463ac 74#ifndef CATACOMB_PARANOIA_H
75# include "paranoia.h"
76#endif
77
a4738812 78/*----- Generic hash function interface -----------------------------------*/
79
80/* --- @GHASH_DEF@ --- *
81 *
82 * Arguments: @PRE, pre@ = prefixes for hash function
83 *
84 * Use: Defines the generic hash instance.
85 */
86
87#define GHASH_DEF(PRE, pre) \
88 \
89static const ghash_ops gops; \
90 \
91typedef struct gctx { \
92 ghash h; \
93 pre##_ctx c; \
a351d052 94 octet buf[PRE##_HASHSZ]; \
a4738812 95} gctx; \
96 \
97static ghash *ghinit(void) \
98{ \
28ef1f45 99 gctx *g = S_CREATE(gctx); \
a4738812 100 g->h.ops = &gops; \
101 pre##_init(&g->c); \
102 return (&g->h); \
103} \
104 \
105static void ghhash(ghash *h, const void *p, size_t sz) \
106{ \
107 gctx *g = (gctx *)h; \
108 pre##_hash(&g->c, p, sz); \
109} \
110 \
a351d052 111static octet *ghdone(ghash *h, void *buf) \
a4738812 112{ \
113 gctx *g = (gctx *)h; \
a351d052 114 if (!buf) \
115 buf = g->buf; \
a4738812 116 pre##_done(&g->c, buf); \
a351d052 117 return (buf); \
a4738812 118} \
119 \
120static void ghdestroy(ghash *h) \
121{ \
122 gctx *g = (gctx *)h; \
f80463ac 123 BURN(*g); \
28ef1f45 124 S_DESTROY(g); \
a4738812 125} \
126 \
d2fdbc2c 127static ghash *ghcopy(ghash *h) \
128{ \
129 gctx *g = (gctx *)h; \
130 gctx *gg = S_CREATE(gctx); \
131 memcpy(gg, g, sizeof(gctx)); \
132 return (&gg->h); \
133} \
134 \
135static const ghash_ops gops = \
136 { &pre, ghhash, ghdone, ghdestroy, ghcopy }; \
28ef1f45 137const gchash pre = { #pre, PRE##_HASHSZ, ghinit };
a4738812 138
139/*----- That's all, folks -------------------------------------------------*/
140
141#ifdef __cplusplus
142 }
143#endif
144
145#endif