chiark / gitweb /
symm/chacha.c: Set the correct nonce size for `xchachaNN'.
[catacomb] / symm / ghash-def.h
1 /* -*-c-*-
2  *
3  * Definitions for generic hash interface
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16  *
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.
21  *
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
28 #ifndef CATACOMB_GHASH_DEF_H
29 #define CATACOMB_GHASH_DEF_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <mLib/bits.h>
38 #include <mLib/sub.h>
39
40 #ifndef CATACOMB_ARENA_H
41 #  include "arena.h"
42 #endif
43
44 #ifndef CATACOMB_GHASH_H
45 #  include "ghash.h"
46 #endif
47
48 #ifndef CATACOMB_PARANOIA_H
49 #  include "paranoia.h"
50 #endif
51
52 /*----- Generic hash function interface -----------------------------------*/
53
54 /* --- @GHASH_DEF@ --- *
55  *
56  * Arguments:   @PRE, pre@ = prefixes for hash function
57  *
58  * Use:         Defines the generic hash instance.
59  */
60
61 #define GHASH_DEF(PRE, pre) GHASH_DEFX(PRE, pre, #pre)
62 #define GHASH_DEFX(PRE, pre, name)                                      \
63                                                                         \
64 static const ghash_ops gops_##pre;                                      \
65                                                                         \
66 typedef struct gctx_##pre {                                             \
67   ghash h;                                                              \
68   pre##_ctx c;                                                          \
69   octet buf[PRE##_HASHSZ];                                              \
70 } gctx_##pre;                                                           \
71                                                                         \
72 static ghash *ghinit_##pre(void)                                        \
73 {                                                                       \
74   gctx_##pre *g = S_CREATE(gctx_##pre);                                 \
75   g->h.ops = &gops_##pre;                                               \
76   pre##_init(&g->c);                                                    \
77   return (&g->h);                                                       \
78 }                                                                       \
79                                                                         \
80 static void ghhash_##pre(ghash *h, const void *p, size_t sz)            \
81 {                                                                       \
82   gctx_##pre *g = (gctx_##pre *)h;                                      \
83   pre##_hash(&g->c, p, sz);                                             \
84 }                                                                       \
85                                                                         \
86 static octet *ghdone_##pre(ghash *h, void *buf)                         \
87 {                                                                       \
88   gctx_##pre *g = (gctx_##pre *)h;                                      \
89   if (!buf)                                                             \
90     buf = g->buf;                                                       \
91   pre##_done(&g->c, buf);                                               \
92   return (buf);                                                         \
93 }                                                                       \
94                                                                         \
95 static void ghdestroy_##pre(ghash *h)                                   \
96 {                                                                       \
97   gctx_##pre *g = (gctx_##pre *)h;                                      \
98   BURN(*g);                                                             \
99   S_DESTROY(g);                                                         \
100 }                                                                       \
101                                                                         \
102 static ghash *ghcopy_##pre(ghash *h)                                    \
103 {                                                                       \
104   gctx_##pre *g = (gctx_##pre *)h;                                      \
105   gctx_##pre *gg = S_CREATE(gctx_##pre);                                \
106   memcpy(gg, g, sizeof(gctx_##pre));                                    \
107   return (&gg->h);                                                      \
108 }                                                                       \
109                                                                         \
110 static const ghash_ops gops_##pre =                                     \
111   { &pre, ghhash_##pre, ghdone_##pre, ghdestroy_##pre, ghcopy_##pre };  \
112 const gchash pre = { name, PRE##_HASHSZ, ghinit_##pre, PRE##_BUFSZ };
113
114 /*----- That's all, folks -------------------------------------------------*/
115
116 #ifdef __cplusplus
117   }
118 #endif
119
120 #endif