Commit | Line | Data |
---|---|---|
aa1082f2 | 1 | /* -*-c-*- |
aa1082f2 | 2 | * |
3 | * Generic hash function interface | |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
aa1082f2 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
aa1082f2 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
aa1082f2 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
aa1082f2 | 28 | #ifndef CATACOMB_GHASH_H |
29 | #define CATACOMB_GHASH_H | |
30 | ||
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Header files ------------------------------------------------------*/ | |
36 | ||
37 | #include <stddef.h> | |
38 | ||
20aa4704 | 39 | #include <mLib/bits.h> |
40 | ||
aa1082f2 | 41 | /*----- Generic hash function interface -----------------------------------*/ |
42 | ||
aa1082f2 | 43 | typedef struct ghash { |
44 | const struct ghash_ops *ops; /* Pointer to hash operations */ | |
45 | } ghash; | |
46 | ||
47 | typedef struct ghash_ops { | |
92d64cb5 | 48 | const struct gchash *c; /* Pointer to hash class */ |
aa1082f2 | 49 | void (*hash)(ghash */*h*/, const void */*p*/, size_t /*sz*/); /* Hash */ |
a351d052 | 50 | octet *(*done)(ghash */*h*/, void */*buf*/); /* Write result */ |
aa1082f2 | 51 | void (*destroy)(ghash */*h*/); /* Destroy hash block */ |
d2fdbc2c | 52 | ghash *(*copy)(ghash */*h*/); /* Make a copy of the hash context */ |
aa1082f2 | 53 | } ghash_ops; |
54 | ||
59919ae4 | 55 | #define GH_INIT(ch) (ch)->init() |
b817bfc6 | 56 | #define GH_CLASS(h) (h)->ops->c |
59919ae4 | 57 | #define GH_HASH(h, p, sz) (h)->ops->hash((h), (p), (sz)) |
58 | #define GH_DONE(h, buf) (h)->ops->done((h), (buf)) | |
59 | #define GH_DESTROY(h) (h)->ops->destroy((h)) | |
60 | #define GH_COPY(h) (h)->ops->copy((h)) | |
61 | ||
f387fcb1 | 62 | #define GH_HASHU_(h, n, W) do { \ |
63 | TY_U##W n_ = (n); octet b_[SZ_##W]; \ | |
64 | STORE##W(b_, n_); GH_HASH((h), b_, SZ_##W); \ | |
dfd1608a | 65 | } while (0) |
66 | #define GH_HASHU8(h, n) GH_HASHU_((h), (n), 8) | |
67 | #define GH_HASHU16(h, n) GH_HASHU_((h), (n), 16) | |
68 | #define GH_HASHU16_B(h, n) GH_HASHU_((h), (n), 16_B) | |
69 | #define GH_HASHU16_L(h, n) GH_HASHU_((h), (n), 16_L) | |
70 | #define GH_HASHU24(h, n) GH_HASHU_((h), (n), 24) | |
71 | #define GH_HASHU24_B(h, n) GH_HASHU_((h), (n), 24_B) | |
72 | #define GH_HASHU24_L(h, n) GH_HASHU_((h), (n), 24_L) | |
73 | #define GH_HASHU32(h, n) GH_HASHU_((h), (n), 32) | |
74 | #define GH_HASHU32_B(h, n) GH_HASHU_((h), (n), 32_B) | |
75 | #define GH_HASHU32_L(h, n) GH_HASHU_((h), (n), 32_L) | |
f387fcb1 | 76 | #ifdef HAVE_UINT64 |
77 | # define GH_HASHU64(h, n) GH_HASHU_((h), (n), 64) | |
78 | # define GH_HASHU64_B(h, n) GH_HASHU_((h), (n), 64_B) | |
79 | # define GH_HASHU64_L(h, n) GH_HASHU_((h), (n), 64_L) | |
80 | #endif | |
dfd1608a | 81 | |
f387fcb1 | 82 | #define GH_HASHBUF_(h, p, sz, W) do { \ |
83 | size_t sz_ = (sz); assert(sz_ <= MASK##W); \ | |
84 | GH_HASHU_(h, sz_, W); GH_HASH(h, (p), sz_); \ | |
dfd1608a | 85 | } while (0) |
86 | #define GH_HASHBUF8(h, p, sz) GH_HASHBUF_((h), (p), (sz), 8) | |
87 | #define GH_HASHBUF16(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16) | |
88 | #define GH_HASHBUF16_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_L) | |
89 | #define GH_HASHBUF16_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 16_B) | |
90 | #define GH_HASHBUF24(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24) | |
91 | #define GH_HASHBUF24_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_L) | |
92 | #define GH_HASHBUF24_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 24_B) | |
93 | #define GH_HASHBUF32(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32) | |
94 | #define GH_HASHBUF32_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_L) | |
95 | #define GH_HASHBUF32_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 32_B) | |
f387fcb1 | 96 | #ifdef HAVE_UINT64 |
97 | # define GH_HASHBUF64(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64) | |
98 | # define GH_HASHBUF64_L(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_L) | |
99 | # define GH_HASHBUF64_B(h, p, sz) GH_HASHBUF_((h), (p), (sz), 64_B) | |
100 | #endif | |
dfd1608a | 101 | |
45c0fd36 | 102 | #define GH_HASHSTR_(h, p, W) do { \ |
f387fcb1 | 103 | const char *p_ = (p); GH_HASHBUF_((h), p_, strlen(p_), W); \ |
dfd1608a | 104 | } while (0) |
105 | #define GH_HASHSTR8(h, p) GH_HASHSTR_((h), (p), 8) | |
106 | #define GH_HASHSTR16(h, p) GH_HASHSTR_((h), (p), 16) | |
107 | #define GH_HASHSTR16_L(h, p) GH_HASHSTR_((h), (p), 16_L) | |
108 | #define GH_HASHSTR16_B(h, p) GH_HASHSTR_((h), (p), 16_B) | |
109 | #define GH_HASHSTR24(h, p) GH_HASHSTR_((h), (p), 24) | |
110 | #define GH_HASHSTR24_L(h, p) GH_HASHSTR_((h), (p), 24_L) | |
111 | #define GH_HASHSTR24_B(h, p) GH_HASHSTR_((h), (p), 24_B) | |
112 | #define GH_HASHSTR32(h, p) GH_HASHSTR_((h), (p), 32) | |
113 | #define GH_HASHSTR32_L(h, p) GH_HASHSTR_((h), (p), 32_L) | |
114 | #define GH_HASHSTR32_B(h, p) GH_HASHSTR_((h), (p), 32_B) | |
f387fcb1 | 115 | #ifdef HAVE_UINT64 |
2a7c5203 MW |
116 | # define GH_HASHSTR64(h, p) GH_HASHSTR_((h), (p), 64) |
117 | # define GH_HASHSTR64_L(h, p) GH_HASHSTR_((h), (p), 64_L) | |
118 | # define GH_HASHSTR64_B(h, p) GH_HASHSTR_((h), (p), 64_B) | |
f387fcb1 | 119 | #endif |
dfd1608a | 120 | |
121 | #define GH_HASHSTRZ(h, p) do { \ | |
122 | const char *p_ = (p); GH_HASH((h), p_, strlen(p_) + 1); \ | |
123 | } while (0) | |
124 | #define GH_HASHSTR(h, p) do { \ | |
125 | const char *p_ = (p); GH_HASH((h), p_, strlen(p_)); \ | |
126 | } while (0) | |
127 | ||
aa1082f2 | 128 | typedef struct gchash { |
92d64cb5 | 129 | const char *name; /* Name of the hash function */ |
130 | size_t hashsz; /* Size of output hash */ | |
aa1082f2 | 131 | ghash *(*init)(void); /* Create a new hash instance */ |
59919ae4 | 132 | size_t bufsz; /* Buffer size, or zero */ |
aa1082f2 | 133 | } gchash; |
134 | ||
59919ae4 | 135 | /*----- Tables ------------------------------------------------------------*/ |
136 | ||
137 | extern const gchash *const ghashtab[]; | |
138 | ||
139 | /* --- @ghash_byname@ --- * | |
140 | * | |
141 | * Arguments: @const char *p@ = pointer to name string | |
142 | * | |
143 | * Returns: The named cipher class, or null. | |
144 | */ | |
145 | ||
146 | extern const gchash *ghash_byname(const char */*p*/); | |
147 | ||
aa1082f2 | 148 | /*----- That's all, folks -------------------------------------------------*/ |
149 | ||
150 | #ifdef __cplusplus | |
151 | } | |
152 | #endif | |
153 | ||
154 | #endif |