Commit | Line | Data |
---|---|---|
aa1082f2 | 1 | /* -*-c-*- |
2 | * | |
dfd1608a | 3 | * $Id$ |
aa1082f2 | 4 | * |
5 | * Generic hash function interface | |
6 | * | |
7 | * (c) 1999 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
aa1082f2 | 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. | |
45c0fd36 | 18 | * |
aa1082f2 | 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. | |
45c0fd36 | 23 | * |
aa1082f2 | 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 | ||
aa1082f2 | 30 | #ifndef CATACOMB_GHASH_H |
31 | #define CATACOMB_GHASH_H | |
32 | ||
33 | #ifdef __cplusplus | |
34 | extern "C" { | |
35 | #endif | |
36 | ||
37 | /*----- Header files ------------------------------------------------------*/ | |
38 | ||
39 | #include <stddef.h> | |
40 | ||
20aa4704 | 41 | #include <mLib/bits.h> |
42 | ||
aa1082f2 | 43 | /*----- Generic hash function interface -----------------------------------*/ |
44 | ||
aa1082f2 | 45 | typedef struct ghash { |
46 | const struct ghash_ops *ops; /* Pointer to hash operations */ | |
47 | } ghash; | |
48 | ||
49 | typedef struct ghash_ops { | |
92d64cb5 | 50 | const struct gchash *c; /* Pointer to hash class */ |
aa1082f2 | 51 | void (*hash)(ghash */*h*/, const void */*p*/, size_t /*sz*/); /* Hash */ |
a351d052 | 52 | octet *(*done)(ghash */*h*/, void */*buf*/); /* Write result */ |
aa1082f2 | 53 | void (*destroy)(ghash */*h*/); /* Destroy hash block */ |
d2fdbc2c | 54 | ghash *(*copy)(ghash */*h*/); /* Make a copy of the hash context */ |
aa1082f2 | 55 | } ghash_ops; |
56 | ||
59919ae4 | 57 | #define GH_INIT(ch) (ch)->init() |
b817bfc6 | 58 | #define GH_CLASS(h) (h)->ops->c |
59919ae4 | 59 | #define GH_HASH(h, p, sz) (h)->ops->hash((h), (p), (sz)) |
60 | #define GH_DONE(h, buf) (h)->ops->done((h), (buf)) | |
61 | #define GH_DESTROY(h) (h)->ops->destroy((h)) | |
62 | #define GH_COPY(h) (h)->ops->copy((h)) | |
63 | ||
f387fcb1 | 64 | #define GH_HASHU_(h, n, W) do { \ |
65 | TY_U##W n_ = (n); octet b_[SZ_##W]; \ | |
66 | STORE##W(b_, n_); GH_HASH((h), b_, SZ_##W); \ | |
dfd1608a | 67 | } while (0) |
68 | #define GH_HASHU8(h, n) GH_HASHU_((h), (n), 8) | |
69 | #define GH_HASHU16(h, n) GH_HASHU_((h), (n), 16) | |
70 | #define GH_HASHU16_B(h, n) GH_HASHU_((h), (n), 16_B) | |
71 | #define GH_HASHU16_L(h, n) GH_HASHU_((h), (n), 16_L) | |
72 | #define GH_HASHU24(h, n) GH_HASHU_((h), (n), 24) | |
73 | #define GH_HASHU24_B(h, n) GH_HASHU_((h), (n), 24_B) | |
74 | #define GH_HASHU24_L(h, n) GH_HASHU_((h), (n), 24_L) | |
75 | #define GH_HASHU32(h, n) GH_HASHU_((h), (n), 32) | |
76 | #define GH_HASHU32_B(h, n) GH_HASHU_((h), (n), 32_B) | |
77 | #define GH_HASHU32_L(h, n) GH_HASHU_((h), (n), 32_L) | |
f387fcb1 | 78 | #ifdef HAVE_UINT64 |
79 | # define GH_HASHU64(h, n) GH_HASHU_((h), (n), 64) | |
80 | # define GH_HASHU64_B(h, n) GH_HASHU_((h), (n), 64_B) | |
81 | # define GH_HASHU64_L(h, n) GH_HASHU_((h), (n), 64_L) | |
82 | #endif | |
dfd1608a | 83 | |
f387fcb1 | 84 | #define GH_HASHBUF_(h, p, sz, W) do { \ |
85 | size_t sz_ = (sz); assert(sz_ <= MASK##W); \ | |
86 | GH_HASHU_(h, sz_, W); GH_HASH(h, (p), sz_); \ | |
dfd1608a | 87 | } while (0) |
88 | #define GH_HASHBUF8(h, p, sz) GH_HASHBUF_((h), (p), (sz), 8) | |
89 | #define GH_HASHBUF16(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16) | |
90 | #define GH_HASHBUF16_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_L) | |
91 | #define GH_HASHBUF16_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_B) | |
92 | #define GH_HASHBUF24(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24) | |
93 | #define GH_HASHBUF24_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_L) | |
94 | #define GH_HASHBUF24_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_B) | |
95 | #define GH_HASHBUF32(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32) | |
96 | #define GH_HASHBUF32_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_L) | |
97 | #define GH_HASHBUF32_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_B) | |
f387fcb1 | 98 | #ifdef HAVE_UINT64 |
99 | # define GH_HASHBUF64(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64) | |
100 | # define GH_HASHBUF64_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_L) | |
101 | # define GH_HASHBUF64_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_B) | |
102 | #endif | |
dfd1608a | 103 | |
45c0fd36 | 104 | #define GH_HASHSTR_(h, p, W) do { \ |
f387fcb1 | 105 | const char *p_ = (p); GH_HASHBUF_((h), p_, strlen(p_), W); \ |
dfd1608a | 106 | } while (0) |
107 | #define GH_HASHSTR8(h, p) GH_HASHSTR_((h), (p), 8) | |
108 | #define GH_HASHSTR16(h, p) GH_HASHSTR_((h), (p), 16) | |
109 | #define GH_HASHSTR16_L(h, p) GH_HASHSTR_((h), (p), 16_L) | |
110 | #define GH_HASHSTR16_B(h, p) GH_HASHSTR_((h), (p), 16_B) | |
111 | #define GH_HASHSTR24(h, p) GH_HASHSTR_((h), (p), 24) | |
112 | #define GH_HASHSTR24_L(h, p) GH_HASHSTR_((h), (p), 24_L) | |
113 | #define GH_HASHSTR24_B(h, p) GH_HASHSTR_((h), (p), 24_B) | |
114 | #define GH_HASHSTR32(h, p) GH_HASHSTR_((h), (p), 32) | |
115 | #define GH_HASHSTR32_L(h, p) GH_HASHSTR_((h), (p), 32_L) | |
116 | #define GH_HASHSTR32_B(h, p) GH_HASHSTR_((h), (p), 32_B) | |
f387fcb1 | 117 | #ifdef HAVE_UINT64 |
2a7c5203 MW |
118 | # define GH_HASHSTR64(h, p) GH_HASHSTR_((h), (p), 64) |
119 | # define GH_HASHSTR64_L(h, p) GH_HASHSTR_((h), (p), 64_L) | |
120 | # define GH_HASHSTR64_B(h, p) GH_HASHSTR_((h), (p), 64_B) | |
f387fcb1 | 121 | #endif |
dfd1608a | 122 | |
123 | #define GH_HASHSTRZ(h, p) do { \ | |
124 | const char *p_ = (p); GH_HASH((h), p_, strlen(p_) + 1); \ | |
125 | } while (0) | |
126 | #define GH_HASHSTR(h, p) do { \ | |
127 | const char *p_ = (p); GH_HASH((h), p_, strlen(p_)); \ | |
128 | } while (0) | |
129 | ||
aa1082f2 | 130 | typedef struct gchash { |
92d64cb5 | 131 | const char *name; /* Name of the hash function */ |
132 | size_t hashsz; /* Size of output hash */ | |
aa1082f2 | 133 | ghash *(*init)(void); /* Create a new hash instance */ |
59919ae4 | 134 | size_t bufsz; /* Buffer size, or zero */ |
aa1082f2 | 135 | } gchash; |
136 | ||
59919ae4 | 137 | /*----- Tables ------------------------------------------------------------*/ |
138 | ||
139 | extern const gchash *const ghashtab[]; | |
140 | ||
141 | /* --- @ghash_byname@ --- * | |
142 | * | |
143 | * Arguments: @const char *p@ = pointer to name string | |
144 | * | |
145 | * Returns: The named cipher class, or null. | |
146 | */ | |
147 | ||
148 | extern const gchash *ghash_byname(const char */*p*/); | |
149 | ||
aa1082f2 | 150 | /*----- That's all, folks -------------------------------------------------*/ |
151 | ||
152 | #ifdef __cplusplus | |
153 | } | |
154 | #endif | |
155 | ||
156 | #endif |