Commit | Line | Data |
---|---|---|
5c3f75ec | 1 | /* -*-c-*- |
2 | * | |
c65df279 | 3 | * $Id$ |
5c3f75ec | 4 | * |
5 | * Catcrypt common stuff | |
6 | * | |
7 | * (c) 2004 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
5c3f75ec | 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 | * |
5c3f75ec | 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 | * |
5c3f75ec | 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 | ||
cd6eca43 MW |
39 | #if _FILE_OFFSET_BITS != 64 |
40 | # error "Must set _FILE_OFFSET_BITS to 64." | |
41 | #endif | |
42 | ||
5c3f75ec | 43 | #include <stdio.h> |
c65df279 | 44 | #include <string.h> |
cd6eca43 | 45 | #include <time.h> |
5c3f75ec | 46 | |
47 | #include <mLib/dstr.h> | |
48 | ||
49 | #include "key.h" | |
50 | #include "gcipher.h" | |
51 | #include "ghash.h" | |
52 | #include "gmac.h" | |
53 | ||
54 | /*----- Data structures ---------------------------------------------------*/ | |
55 | ||
cd6eca43 MW |
56 | /* --- Progress indicators --- */ |
57 | ||
58 | typedef struct fprogress { | |
59 | const char *bp; | |
60 | off_t o, sz, olast; | |
61 | time_t start, last; | |
62 | char name[24]; | |
63 | } fprogress; | |
64 | ||
5c3f75ec | 65 | /* --- Key encapsulation --- */ |
66 | ||
67 | typedef struct kem { | |
68 | const struct kemops *ops; | |
69 | key_packdef *kp; | |
70 | void *kd; | |
71 | const gchash *h; | |
72 | const gccipher *c, *cx; | |
73 | const gcmac *m; | |
74 | } kem; | |
75 | ||
76 | typedef struct kemops { | |
77 | const key_fetchdef *kf; /* Key fetching structure */ | |
78 | size_t kdsz; /* Size of the key-data structure */ | |
79 | kem *(*init)(key */*k*/, void */*kd*/); | |
80 | int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/); | |
81 | const char *(*check)(kem */*k*/); | |
82 | void (*destroy)(kem */*k*/); | |
83 | } kemops; | |
84 | ||
c65df279 | 85 | struct kemtab { |
86 | const char *name; | |
87 | const kemops *encops; | |
88 | const kemops *decops; | |
89 | }; | |
90 | ||
91 | extern const struct kemtab kemtab[]; | |
92 | ||
5c3f75ec | 93 | /* --- Signing --- */ |
94 | ||
95 | typedef struct sig { | |
96 | const struct sigops *ops; | |
97 | key_packdef *kp; | |
98 | void *kd; | |
78ec50fa | 99 | const gchash *ch; |
5c3f75ec | 100 | ghash *h; |
101 | } sig; | |
102 | ||
103 | typedef struct sigops { | |
104 | const key_fetchdef *kf; /* Key fetching structure */ | |
105 | size_t kdsz; /* Size of the key-data structure */ | |
106 | sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/); | |
107 | int (*doit)(sig */*s*/, dstr */*d*/); | |
108 | const char *(*check)(sig */*s*/); | |
109 | void (*destroy)(sig */*s*/); | |
110 | } sigops; | |
111 | ||
c65df279 | 112 | struct sigtab { |
113 | const char *name; | |
114 | const sigops *signops; | |
115 | const sigops *verifyops; | |
116 | const gchash *ch; | |
117 | }; | |
45c0fd36 | 118 | |
c65df279 | 119 | extern const struct sigtab sigtab[]; |
120 | ||
5c3f75ec | 121 | /* --- Data encoding --- */ |
122 | ||
123 | typedef struct enc { | |
124 | const struct encops *ops; | |
125 | FILE *fp; | |
126 | } enc; | |
127 | ||
128 | typedef struct encops { | |
129 | const char *name; | |
130 | const char *rmode, *wmode; | |
cd6eca43 | 131 | int nraw, ncook; |
5c3f75ec | 132 | enc *(*initenc)(FILE */*fp*/, const char */*msg*/); |
fa54fe1e | 133 | enc *(*initdec)(FILE */*fp*/, |
134 | int (*/*func*/)(const char *, void *), void */*p*/); | |
5c3f75ec | 135 | int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/); |
136 | int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/); | |
137 | int (*encdone)(enc */*e*/); | |
138 | int (*decdone)(enc */*e*/); | |
139 | void (*destroy)(enc */*e*/); | |
140 | } encops; | |
141 | ||
c65df279 | 142 | extern const encops enctab[]; |
143 | ||
5c3f75ec | 144 | /*----- Functions provided ------------------------------------------------*/ |
145 | ||
146 | /* --- @getkem@ --- * | |
147 | * | |
148 | * Arguments: @key *k@ = the key to load | |
149 | * @const char *app@ = application name | |
150 | * @int wantpriv@ = nonzero if we want to decrypt | |
151 | * | |
152 | * Returns: A key-encapsulating thing. | |
153 | * | |
154 | * Use: Loads a key. | |
155 | */ | |
156 | ||
157 | extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/); | |
158 | ||
159 | /* --- @setupkem@ --- * | |
160 | * | |
161 | * Arguments: @kem *k@ = key-encapsulation thing | |
162 | * @dstr *d@ = key-encapsulation data | |
163 | * @gcipher **cx@ = key-expansion function (for IVs) | |
164 | * @gcipher **c@ = where to put initialized encryption scheme | |
165 | * @gmac **m@ = where to put initialized MAC | |
166 | * | |
167 | * Returns: Zero for success, nonzero on faliure. | |
168 | * | |
169 | * Use: Initializes all the various symmetric things from a KEM. | |
170 | */ | |
171 | ||
172 | extern int setupkem(kem */*k*/, dstr */*d*/, | |
173 | gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/); | |
174 | ||
175 | /* --- @freekem@ --- * | |
176 | * | |
177 | * Arguments: @kem *k@ = key-encapsulation thing | |
178 | * | |
179 | * Returns: --- | |
180 | * | |
181 | * Use: Frees up a key-encapsulation thing. | |
182 | */ | |
183 | ||
184 | extern void freekem(kem */*k*/); | |
185 | ||
186 | /* --- @getsig@ --- * | |
187 | * | |
188 | * Arguments: @key *k@ = the key to load | |
189 | * @const char *app@ = application name | |
190 | * @int wantpriv@ = nonzero if we want to sign | |
191 | * | |
192 | * Returns: A signature-making thing. | |
193 | * | |
194 | * Use: Loads a key and starts hashing. | |
195 | */ | |
196 | ||
197 | extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/); | |
198 | ||
199 | /* --- @freesig@ --- * | |
200 | * | |
201 | * Arguments: @sig *s@ = signature-making thing | |
202 | * | |
203 | * Returns: --- | |
204 | * | |
205 | * Use: Frees up a signature-making thing | |
206 | */ | |
207 | ||
208 | extern void freesig(sig */*s*/); | |
209 | ||
210 | /* --- @getenc@ --- * | |
211 | * | |
212 | * Arguments: @const char *enc@ = name of wanted encoding | |
213 | * | |
214 | * Returns: Pointer to encoder operations. | |
215 | * | |
216 | * Use: Finds a named encoder or decoder. | |
217 | */ | |
218 | ||
219 | extern const encops *getenc(const char */*enc*/); | |
220 | ||
fa54fe1e | 221 | /* --- @checkbdry@ --- * |
222 | * | |
223 | * Arguments: @const char *b@ = boundary string found | |
224 | * @void *p@ = boundary string wanted | |
225 | * | |
226 | * Returns: Nonzero if the boundary string is the one we wanted. | |
227 | * | |
228 | * Use: Pass as @func@ to @initdec@ if you just want a simple life. | |
229 | */ | |
230 | ||
231 | extern int checkbdry(const char */*b*/, void */*p*/); | |
232 | ||
5c3f75ec | 233 | /* --- @initenc@ --- * |
234 | * | |
235 | * Arguments: @const encops *eo@ = operations (from @getenc@) | |
236 | * @FILE *fp@ = file handle to attach | |
237 | * @const char *msg@ = banner message | |
5c3f75ec | 238 | * |
239 | * Returns: The encoder object. | |
240 | * | |
241 | * Use: Initializes an encoder. | |
242 | */ | |
243 | ||
fa54fe1e | 244 | extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/); |
245 | ||
246 | /* --- @initdec@ --- * | |
247 | * | |
248 | * Arguments: @const encops *eo@ = operations (from @getenc@) | |
249 | * @FILE *fp@ = file handle to attach | |
250 | * @int (*func)(const char *, void *)@ = banner check function | |
251 | * @void *p@ = argument for @func@ | |
252 | * | |
253 | * Returns: The encoder object. | |
254 | * | |
255 | * Use: Initializes an encoder. | |
256 | */ | |
257 | ||
258 | extern enc *initdec(const encops */*eo*/, FILE */*fp*/, | |
259 | int (*/*func*/)(const char *, void *), void */*p*/); | |
5c3f75ec | 260 | |
261 | /* --- @freeenc@ --- * | |
262 | * | |
263 | * Arguments: @enc *e@ = encoder object | |
264 | * | |
265 | * Returns: --- | |
266 | * | |
267 | * Use: Frees an encoder object. | |
268 | */ | |
269 | ||
270 | extern void freeenc(enc */*e*/); | |
271 | ||
fa54fe1e | 272 | /* --- @cmd_encode@, @cmd_decode@ --- */ |
273 | ||
274 | #define CMD_ENCODE { \ | |
275 | "encode", cmd_encode, \ | |
cd6eca43 | 276 | "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \ |
fa54fe1e | 277 | "\ |
278 | Options:\n\ | |
279 | \n\ | |
280 | -f, --format=FORMAT Encode to FORMAT.\n\ | |
281 | -b, --boundary=LABEL PEM boundary is LABEL.\n\ | |
282 | -o, --output=FILE Write output to FILE.\n\ | |
cd6eca43 | 283 | -p, --progress Show progress on large files.\n\ |
fa54fe1e | 284 | " } |
285 | ||
286 | #define CMD_DECODE { \ | |
287 | "decode", cmd_decode, \ | |
cd6eca43 | 288 | "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \ |
fa54fe1e | 289 | "\ |
290 | Options:\n\ | |
291 | \n\ | |
292 | -f, --format=FORMAT Decode from FORMAT.\n\ | |
293 | -b, --boundary=LABEL PEM boundary is LABEL.\n\ | |
294 | -o, --output=FILE Write output to FILE.\n\ | |
cd6eca43 | 295 | -p, --progress Show progress on large files.\n\ |
fa54fe1e | 296 | " } |
297 | ||
298 | extern int cmd_encode(int /*argc*/, char */*argv*/[]); | |
299 | extern int cmd_decode(int /*argc*/, char */*argv*/[]); | |
300 | ||
c65df279 | 301 | /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- * |
302 | * | |
303 | * Produce list of things. Requires @i@ and @w@ variables in scope. | |
304 | * END-TEST and NAME-EXPR are in terms of @i@. | |
305 | */ | |
306 | ||
307 | #define LIST(what, fp, end, name) do { \ | |
308 | fputs(what ":\n ", fp); \ | |
309 | w = 2; \ | |
310 | for (i = 0; end; i++) { \ | |
311 | if (w == 2) \ | |
312 | w += strlen(name); \ | |
313 | else { \ | |
314 | if (strlen(name) + w > 76) { \ | |
315 | fputs("\n ", fp); \ | |
316 | w = 2 + strlen(name); \ | |
317 | } else { \ | |
318 | fputc(' ', fp); \ | |
319 | w += strlen(name) + 1; \ | |
320 | } \ | |
321 | } \ | |
322 | fputs(name, fp); \ | |
323 | } \ | |
324 | fputc('\n', fp); \ | |
325 | } while (0) | |
326 | ||
327 | #define STDLISTS(LI) \ | |
328 | LI("Hash functions", hash, \ | |
329 | ghashtab[i], ghashtab[i]->name) \ | |
330 | LI("Encryption schemes", enc, \ | |
331 | gciphertab[i], gciphertab[i]->name) \ | |
332 | LI("Message authentication schemes", mac, \ | |
333 | gmactab[i], gmactab[i]->name) \ | |
334 | LI("Elliptic curves", ec, \ | |
335 | ectab[i].name, ectab[i].name) \ | |
336 | LI("Diffie-Hellman groups", dh, \ | |
337 | ptab[i].name, ptab[i].name) | |
338 | ||
339 | #define LIDECL(text, tag, test, name) \ | |
340 | static void show_##tag(void); | |
341 | ||
342 | #define LIDEF(text, tag, test, name) \ | |
343 | static void show_##tag(void) \ | |
344 | { \ | |
345 | unsigned i, w; \ | |
346 | LIST(text, stdout, test, name); \ | |
347 | } | |
348 | ||
349 | #define LIENT(text, tag, test, name) \ | |
350 | { #tag, show_##tag }, | |
351 | ||
352 | struct listent { | |
353 | const char *name; | |
354 | void (*list)(void); | |
355 | }; | |
356 | ||
357 | #define MAKELISTTAB(listtab, LISTS) \ | |
358 | LISTS(LIDECL) \ | |
359 | static const struct listent listtab[] = { \ | |
360 | LISTS(LIENT) \ | |
361 | { 0, 0 } \ | |
362 | }; \ | |
363 | LISTS(LIDEF) | |
364 | ||
365 | extern int displaylists(const struct listent */*listtab*/, | |
366 | char *const /*argv*/[]); | |
367 | ||
368 | /*----- Subcommand dispatch -----------------------------------------------*/ | |
369 | ||
370 | typedef struct cmd { | |
371 | const char *name; | |
372 | int (*cmd)(int /*argc*/, char */*argv*/[]); | |
373 | const char *usage; | |
374 | const char *help; | |
375 | } cmd; | |
376 | ||
377 | extern void version(FILE */*fp*/); | |
378 | extern void help_global(FILE */*fp*/); | |
379 | ||
380 | /* --- @findcmd@ --- * | |
381 | * | |
382 | * Arguments: @const cmd *cmds@ = pointer to command table | |
383 | * @const char *name@ = a command name | |
384 | * | |
385 | * Returns: Pointer to the command structure. | |
386 | * | |
387 | * Use: Looks up a command by name. If the command isn't found, an | |
388 | * error is reported and the program is terminated. | |
389 | */ | |
390 | ||
391 | const cmd *findcmd(const cmd */*cmds*/, const char */*name*/); | |
392 | ||
393 | /* --- @sc_help@ --- * | |
394 | * | |
395 | * Arguments: @const cmd *cmds@ = pointer to command table | |
396 | * @FILE *fp@ = output file handle | |
397 | * @char *const *argv@ = remaining arguments | |
398 | * | |
399 | * Returns: --- | |
400 | * | |
401 | * Use: Prints a help message, maybe with help about subcommands. | |
402 | */ | |
403 | ||
404 | extern void sc_help(const cmd */*cmds*/, FILE */*fp*/, | |
405 | char *const */*argv*/); | |
406 | ||
cd6eca43 MW |
407 | /*----- Progress indicators -----------------------------------------------*/ |
408 | ||
409 | /* --- @fprogress_init@ --- * | |
410 | * | |
411 | * Arguments: @fprogress *f@ = progress context to be initialized | |
412 | * @const char *name@ = file name string to show | |
413 | * @FILE *fp@ = file we're reading from | |
414 | * | |
415 | * Returns: Zero on success, nonzero if the file's state is now broken. | |
416 | * | |
417 | * Use: Initializes a progress context. Nothing is actually | |
418 | * displayed yet. | |
419 | */ | |
420 | ||
421 | extern int fprogress_init(fprogress */*f*/, | |
422 | const char */*name*/, FILE */*fp*/); | |
423 | ||
424 | /* --- @fprogress_update@ --- * | |
425 | * | |
426 | * Arguments: @fprogress *f@ = progress context | |
427 | * @size_t n@ = how much progress has been made | |
428 | * | |
429 | * Returns: --- | |
430 | * | |
431 | * Use: Maybe updates the display to show that some progress has been | |
432 | * made. | |
433 | */ | |
434 | ||
435 | extern void fprogress_update(fprogress */*f*/, size_t /*n*/); | |
436 | ||
437 | /* --- @fprogress_clear@ --- * | |
438 | * | |
439 | * Arguments: @fprogress *f@ = progress context | |
440 | * | |
441 | * Returns: --- | |
442 | * | |
443 | * Use: Clears the progress display from the screen. | |
444 | */ | |
445 | ||
446 | extern void fprogress_clear(fprogress */*f*/); | |
447 | ||
448 | /* --- @fprogress_done@ --- * | |
449 | * | |
450 | * Arguments: @fprogress *f@ = progress context | |
451 | * | |
452 | * Returns: --- | |
453 | * | |
454 | * Use: Clear up the progress context and removes any display. | |
455 | */ | |
456 | ||
457 | extern void fprogress_done(fprogress */*f*/); | |
458 | ||
5c3f75ec | 459 | /*----- That's all, folks -------------------------------------------------*/ |
460 | ||
461 | #ifdef __cplusplus | |
462 | } | |
463 | #endif | |
464 | ||
465 | #endif |