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