5c3f75ec |
1 | /* -*-c-*- |
2 | * |
c65df279 |
3 | * $Id$ |
5c3f75ec |
4 | * |
5 | * Catcrypt common stuff |
6 | * |
7 | * (c) 2004 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 | #ifndef CATACOMB_CC_H |
31 | #define CATACOMB_CC_H |
32 | |
33 | #ifdef __cplusplus |
34 | extern "C" { |
35 | #endif |
36 | |
37 | /*----- Header files ------------------------------------------------------*/ |
38 | |
39 | #include <stdio.h> |
c65df279 |
40 | #include <string.h> |
5c3f75ec |
41 | |
42 | #include <mLib/dstr.h> |
43 | |
44 | #include "key.h" |
45 | #include "gcipher.h" |
46 | #include "ghash.h" |
47 | #include "gmac.h" |
48 | |
49 | /*----- Data structures ---------------------------------------------------*/ |
50 | |
51 | /* --- Key encapsulation --- */ |
52 | |
53 | typedef struct kem { |
54 | const struct kemops *ops; |
55 | key_packdef *kp; |
56 | void *kd; |
57 | const gchash *h; |
58 | const gccipher *c, *cx; |
59 | const gcmac *m; |
60 | } kem; |
61 | |
62 | typedef struct kemops { |
63 | const key_fetchdef *kf; /* Key fetching structure */ |
64 | size_t kdsz; /* Size of the key-data structure */ |
65 | kem *(*init)(key */*k*/, void */*kd*/); |
66 | int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/); |
67 | const char *(*check)(kem */*k*/); |
68 | void (*destroy)(kem */*k*/); |
69 | } kemops; |
70 | |
c65df279 |
71 | struct kemtab { |
72 | const char *name; |
73 | const kemops *encops; |
74 | const kemops *decops; |
75 | }; |
76 | |
77 | extern const struct kemtab kemtab[]; |
78 | |
5c3f75ec |
79 | /* --- Signing --- */ |
80 | |
81 | typedef struct sig { |
82 | const struct sigops *ops; |
83 | key_packdef *kp; |
84 | void *kd; |
85 | ghash *h; |
86 | } sig; |
87 | |
88 | typedef struct sigops { |
89 | const key_fetchdef *kf; /* Key fetching structure */ |
90 | size_t kdsz; /* Size of the key-data structure */ |
91 | sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/); |
92 | int (*doit)(sig */*s*/, dstr */*d*/); |
93 | const char *(*check)(sig */*s*/); |
94 | void (*destroy)(sig */*s*/); |
95 | } sigops; |
96 | |
c65df279 |
97 | struct sigtab { |
98 | const char *name; |
99 | const sigops *signops; |
100 | const sigops *verifyops; |
101 | const gchash *ch; |
102 | }; |
103 | |
104 | extern const struct sigtab sigtab[]; |
105 | |
5c3f75ec |
106 | /* --- Data encoding --- */ |
107 | |
108 | typedef struct enc { |
109 | const struct encops *ops; |
110 | FILE *fp; |
111 | } enc; |
112 | |
113 | typedef struct encops { |
114 | const char *name; |
115 | const char *rmode, *wmode; |
116 | enc *(*initenc)(FILE */*fp*/, const char */*msg*/); |
117 | enc *(*initdec)(FILE */*fp*/, const char */*msg*/); |
118 | int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/); |
119 | int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/); |
120 | int (*encdone)(enc */*e*/); |
121 | int (*decdone)(enc */*e*/); |
122 | void (*destroy)(enc */*e*/); |
123 | } encops; |
124 | |
c65df279 |
125 | extern const encops enctab[]; |
126 | |
5c3f75ec |
127 | /*----- Functions provided ------------------------------------------------*/ |
128 | |
129 | /* --- @getkem@ --- * |
130 | * |
131 | * Arguments: @key *k@ = the key to load |
132 | * @const char *app@ = application name |
133 | * @int wantpriv@ = nonzero if we want to decrypt |
134 | * |
135 | * Returns: A key-encapsulating thing. |
136 | * |
137 | * Use: Loads a key. |
138 | */ |
139 | |
140 | extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/); |
141 | |
142 | /* --- @setupkem@ --- * |
143 | * |
144 | * Arguments: @kem *k@ = key-encapsulation thing |
145 | * @dstr *d@ = key-encapsulation data |
146 | * @gcipher **cx@ = key-expansion function (for IVs) |
147 | * @gcipher **c@ = where to put initialized encryption scheme |
148 | * @gmac **m@ = where to put initialized MAC |
149 | * |
150 | * Returns: Zero for success, nonzero on faliure. |
151 | * |
152 | * Use: Initializes all the various symmetric things from a KEM. |
153 | */ |
154 | |
155 | extern int setupkem(kem */*k*/, dstr */*d*/, |
156 | gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/); |
157 | |
158 | /* --- @freekem@ --- * |
159 | * |
160 | * Arguments: @kem *k@ = key-encapsulation thing |
161 | * |
162 | * Returns: --- |
163 | * |
164 | * Use: Frees up a key-encapsulation thing. |
165 | */ |
166 | |
167 | extern void freekem(kem */*k*/); |
168 | |
169 | /* --- @getsig@ --- * |
170 | * |
171 | * Arguments: @key *k@ = the key to load |
172 | * @const char *app@ = application name |
173 | * @int wantpriv@ = nonzero if we want to sign |
174 | * |
175 | * Returns: A signature-making thing. |
176 | * |
177 | * Use: Loads a key and starts hashing. |
178 | */ |
179 | |
180 | extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/); |
181 | |
182 | /* --- @freesig@ --- * |
183 | * |
184 | * Arguments: @sig *s@ = signature-making thing |
185 | * |
186 | * Returns: --- |
187 | * |
188 | * Use: Frees up a signature-making thing |
189 | */ |
190 | |
191 | extern void freesig(sig */*s*/); |
192 | |
193 | /* --- @getenc@ --- * |
194 | * |
195 | * Arguments: @const char *enc@ = name of wanted encoding |
196 | * |
197 | * Returns: Pointer to encoder operations. |
198 | * |
199 | * Use: Finds a named encoder or decoder. |
200 | */ |
201 | |
202 | extern const encops *getenc(const char */*enc*/); |
203 | |
204 | /* --- @initenc@ --- * |
205 | * |
206 | * Arguments: @const encops *eo@ = operations (from @getenc@) |
207 | * @FILE *fp@ = file handle to attach |
208 | * @const char *msg@ = banner message |
209 | * @int wantenc@ = nonzero if we want to encode |
210 | * |
211 | * Returns: The encoder object. |
212 | * |
213 | * Use: Initializes an encoder. |
214 | */ |
215 | |
216 | extern enc *initenc(const encops */*eo*/, FILE */*fp*/, |
217 | const char */*msg*/, int /*wantenc*/); |
218 | |
219 | /* --- @freeenc@ --- * |
220 | * |
221 | * Arguments: @enc *e@ = encoder object |
222 | * |
223 | * Returns: --- |
224 | * |
225 | * Use: Frees an encoder object. |
226 | */ |
227 | |
228 | extern void freeenc(enc */*e*/); |
229 | |
c65df279 |
230 | /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- * |
231 | * |
232 | * Produce list of things. Requires @i@ and @w@ variables in scope. |
233 | * END-TEST and NAME-EXPR are in terms of @i@. |
234 | */ |
235 | |
236 | #define LIST(what, fp, end, name) do { \ |
237 | fputs(what ":\n ", fp); \ |
238 | w = 2; \ |
239 | for (i = 0; end; i++) { \ |
240 | if (w == 2) \ |
241 | w += strlen(name); \ |
242 | else { \ |
243 | if (strlen(name) + w > 76) { \ |
244 | fputs("\n ", fp); \ |
245 | w = 2 + strlen(name); \ |
246 | } else { \ |
247 | fputc(' ', fp); \ |
248 | w += strlen(name) + 1; \ |
249 | } \ |
250 | } \ |
251 | fputs(name, fp); \ |
252 | } \ |
253 | fputc('\n', fp); \ |
254 | } while (0) |
255 | |
256 | #define STDLISTS(LI) \ |
257 | LI("Hash functions", hash, \ |
258 | ghashtab[i], ghashtab[i]->name) \ |
259 | LI("Encryption schemes", enc, \ |
260 | gciphertab[i], gciphertab[i]->name) \ |
261 | LI("Message authentication schemes", mac, \ |
262 | gmactab[i], gmactab[i]->name) \ |
263 | LI("Elliptic curves", ec, \ |
264 | ectab[i].name, ectab[i].name) \ |
265 | LI("Diffie-Hellman groups", dh, \ |
266 | ptab[i].name, ptab[i].name) |
267 | |
268 | #define LIDECL(text, tag, test, name) \ |
269 | static void show_##tag(void); |
270 | |
271 | #define LIDEF(text, tag, test, name) \ |
272 | static void show_##tag(void) \ |
273 | { \ |
274 | unsigned i, w; \ |
275 | LIST(text, stdout, test, name); \ |
276 | } |
277 | |
278 | #define LIENT(text, tag, test, name) \ |
279 | { #tag, show_##tag }, |
280 | |
281 | struct listent { |
282 | const char *name; |
283 | void (*list)(void); |
284 | }; |
285 | |
286 | #define MAKELISTTAB(listtab, LISTS) \ |
287 | LISTS(LIDECL) \ |
288 | static const struct listent listtab[] = { \ |
289 | LISTS(LIENT) \ |
290 | { 0, 0 } \ |
291 | }; \ |
292 | LISTS(LIDEF) |
293 | |
294 | extern int displaylists(const struct listent */*listtab*/, |
295 | char *const /*argv*/[]); |
296 | |
297 | /*----- Subcommand dispatch -----------------------------------------------*/ |
298 | |
299 | typedef struct cmd { |
300 | const char *name; |
301 | int (*cmd)(int /*argc*/, char */*argv*/[]); |
302 | const char *usage; |
303 | const char *help; |
304 | } cmd; |
305 | |
306 | extern void version(FILE */*fp*/); |
307 | extern void help_global(FILE */*fp*/); |
308 | |
309 | /* --- @findcmd@ --- * |
310 | * |
311 | * Arguments: @const cmd *cmds@ = pointer to command table |
312 | * @const char *name@ = a command name |
313 | * |
314 | * Returns: Pointer to the command structure. |
315 | * |
316 | * Use: Looks up a command by name. If the command isn't found, an |
317 | * error is reported and the program is terminated. |
318 | */ |
319 | |
320 | const cmd *findcmd(const cmd */*cmds*/, const char */*name*/); |
321 | |
322 | /* --- @sc_help@ --- * |
323 | * |
324 | * Arguments: @const cmd *cmds@ = pointer to command table |
325 | * @FILE *fp@ = output file handle |
326 | * @char *const *argv@ = remaining arguments |
327 | * |
328 | * Returns: --- |
329 | * |
330 | * Use: Prints a help message, maybe with help about subcommands. |
331 | */ |
332 | |
333 | extern void sc_help(const cmd */*cmds*/, FILE */*fp*/, |
334 | char *const */*argv*/); |
335 | |
5c3f75ec |
336 | /*----- That's all, folks -------------------------------------------------*/ |
337 | |
338 | #ifdef __cplusplus |
339 | } |
340 | #endif |
341 | |
342 | #endif |