Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / cc.h
1 /* -*-c-*-
2 *
3 * $Id$
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 #if _FILE_OFFSET_BITS != 64
40 # error "Must set _FILE_OFFSET_BITS to 64."
41 #endif
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <time.h>
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 /*----- Cryptographic object tables ---------------------------------------*/
55
56 /* --- Key encapsulation --- */
57
58 typedef 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
67 typedef 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
76 struct kemtab {
77 const char *name;
78 const kemops *encops;
79 const kemops *decops;
80 };
81
82 extern const struct kemtab kemtab[];
83
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
95 extern 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
110 extern 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
122 extern void freekem(kem */*k*/);
123
124 /* --- Signing --- */
125
126 typedef struct sig {
127 const struct sigops *ops;
128 key_packdef *kp;
129 void *kd;
130 const gchash *ch;
131 ghash *h;
132 } sig;
133
134 typedef 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
143 struct sigtab {
144 const char *name;
145 const sigops *signops;
146 const sigops *verifyops;
147 const gchash *ch;
148 };
149
150 extern const struct sigtab sigtab[];
151
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
163 extern 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
174 extern void freesig(sig */*s*/);
175
176 /*----- File encodings ----------------------------------------------------*/
177
178 /* --- Data encoding --- */
179
180 typedef struct enc {
181 const struct encops *ops;
182 FILE *fp;
183 } enc;
184
185 typedef 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
199 extern const encops enctab[];
200
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
210 extern const encops *getenc(const char */*enc*/);
211
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
222 extern int checkbdry(const char */*b*/, void */*p*/);
223
224 /* --- @initenc@ --- *
225 *
226 * Arguments: @const encops *eo@ = operations (from @getenc@)
227 * @FILE *fp@ = file handle to attach
228 * @const char *msg@ = banner message
229 *
230 * Returns: The encoder object.
231 *
232 * Use: Initializes an encoder.
233 */
234
235 extern 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
249 extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
250 int (*/*func*/)(const char *, void *), void */*p*/);
251
252 /* --- @freeenc@ --- *
253 *
254 * Arguments: @enc *e@ = encoder object
255 *
256 * Returns: ---
257 *
258 * Use: Frees an encoder object.
259 */
260
261 extern void freeenc(enc */*e*/);
262
263 /* --- @cmd_encode@, @cmd_decode@ --- */
264
265 #define CMD_ENCODE { \
266 "encode", cmd_encode, \
267 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
268 "\
269 Options:\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\
274 -p, --progress Show progress on large files.\n\
275 " }
276
277 #define CMD_DECODE { \
278 "decode", cmd_decode, \
279 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
280 "\
281 Options:\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\
286 -p, --progress Show progress on large files.\n\
287 " }
288
289 extern int cmd_encode(int /*argc*/, char */*argv*/[]);
290 extern int cmd_decode(int /*argc*/, char */*argv*/[]);
291
292 /*----- Hash encoding functions -------------------------------------------*/
293
294 /* --- Table --- */
295
296 #define ENCODINGS(_) \
297 _(HEX, hex) \
298 _(BASE64, base64) \
299 _(BASE32, base32)
300
301 enum {
302 #define ENUM(tag, name) ENC_##tag,
303 ENCODINGS(ENUM)
304 #undef ENUM
305 ENC_LIMIT
306 };
307
308 typedef struct encodeops {
309 const char *name;
310 void (*put)(const octet *, size_t, FILE *);
311 size_t (*get)(const char *, octet *, size_t, char **);
312 } encodeops;
313
314 extern const encodeops encodingtab[];
315
316 /* --- @getencoding@ --- *
317 *
318 * Arguments: @const char *ename@ = encoding name
319 *
320 * Returns: Pointer to encoding table entry, or null.
321 *
322 * Use: Finds an encoding entry given its name.
323 */
324
325 extern const encodeops *getencoding(const char */*ename*/);
326
327 /*----- File hashing ------------------------------------------------------*/
328
329 typedef struct fhashstate {
330 const gchash *gch;
331 unsigned f;
332 } fhashstate;
333
334 #define FHF_BINARY 0x100u
335 #define FHF_PROGRESS 0x200u
336
337 #define FHF_MASK 0xff00u
338
339 /* --- @gethash@ --- *
340 *
341 * Arguments: @const char *name@ = pointer to name string
342 *
343 * Returns: Pointer to appropriate hash class.
344 *
345 * Use: Chooses a hash function by name.
346 */
347
348 extern const gchash *gethash(const char */*name*/);
349
350 /* --- @fhash_init@ ---*
351 *
352 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
353 * @const gchash *gch@ = hash class to set
354 * @unsigned f@ initial flags to set
355 *
356 * Returns: ---
357 *
358 * Use: Initializes an @fhashstate@ structure.
359 */
360
361 extern void fhash_init(fhashstate */*fh*/,
362 const gchash */*gch*/, unsigned /*f*/);
363
364 /* --- @fhash_free@ --- *
365 *
366 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
367 *
368 * Returns: ---
369 *
370 * Use: Frees an fhash state.
371 */
372
373 extern void fhash_free(fhashstate */*fh*/);
374
375 /* --- @fhash@ --- *
376 *
377 * Arguments: @fhashstate *fh@ = pointer to fhash state
378 * @const char *file@ = file name to be hashed (null for stdin)
379 * @void *buf@ = pointer to hash output buffer
380 *
381 * Returns: Zero if it worked, nonzero on error.
382 *
383 * Use: Hashes a file.
384 */
385
386 extern int fhash(fhashstate */*fh*/, const char */*file*/, void */*buf*/);
387
388 /* --- @hfparse@ --- *
389 *
390 * Arguments: @hfpctx *hfp@ = pointer to the context structure
391 *
392 * Returns: A code indicating what happened.
393 *
394 * Use: Parses a line from the input file.
395 */
396
397 enum { /* Meaning and members set */
398 HF_FILE, /* File hash: @dline@ and @hbuf@ */
399 HF_ENC, /* Encoding: @ee@ */
400 HF_HASH, /* Hash function: @gch@ */
401 HF_ESC, /* Name escape: @f@ */
402 HF_EOF, /* End of file */
403 HF_BAD /* Unrecognized line */
404 };
405
406 typedef struct hfpctx {
407 unsigned f; /* Flags to read */
408 #define HFF_ESCAPE 1u /* File names are escaped */
409 FILE *fp; /* Input file to read */
410 dstr *dline; /* Line contents, corrupted */
411 const gchash *gch; /* Hash function to use */
412 const encodeops *ee; /* Encoding to apply to hashes */
413 dstr *dfile; /* File name for @HF_FILE@ lines */
414 octet *hbuf; /* Output buffer for hash data */
415 } hfpctx;
416
417 extern int hfparse(hfpctx */*hfp*/);
418
419 /*----- String I/O --------------------------------------------------------*/
420
421 #define GSF_RAW 4096u
422 #define GSF_FILE 0u
423 #define GSF_STRING 8192u
424
425 #define GSF_MASK 61440u
426
427 /* --- @getstring@ --- *
428 *
429 * Arguments: @void *in@ = input source
430 * @dstr *d@ = destination string
431 * @unsigned f@ = input flags
432 *
433 * Returns: Zero if OK, nonzero on end-of-file.
434 *
435 * Use: Reads a filename (or something similar) from a stream.
436 */
437
438 extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/);
439
440 /* --- @putstring@ --- *
441 *
442 * Arguments: @FILE *fp@ = stream to write on
443 * @const char *p@ = pointer to text
444 * @unsigned f@ = output flags
445 *
446 * Returns: ---
447 *
448 * Use: Emits a string to a stream.
449 */
450
451 extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/);
452
453 /*----- Lists of things ---------------------------------------------------*/
454
455 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
456 *
457 * Produce list of things. Requires @i@ and @w@ variables in scope.
458 * END-TEST and NAME-EXPR are in terms of @i@.
459 */
460
461 #define LIST(what, fp, end, name) do { \
462 fputs(what ":\n ", fp); \
463 w = 2; \
464 for (i = 0; end; i++) { \
465 if (w == 2) \
466 w += strlen(name); \
467 else { \
468 if (strlen(name) + w > 76) { \
469 fputs("\n ", fp); \
470 w = 2 + strlen(name); \
471 } else { \
472 fputc(' ', fp); \
473 w += strlen(name) + 1; \
474 } \
475 } \
476 fputs(name, fp); \
477 } \
478 fputc('\n', fp); \
479 } while (0)
480
481 #define STDLISTS(LI) \
482 LI("Hash functions", hash, \
483 ghashtab[i], ghashtab[i]->name) \
484 LI("Encryption schemes", enc, \
485 gciphertab[i], gciphertab[i]->name) \
486 LI("Message authentication schemes", mac, \
487 gmactab[i], gmactab[i]->name) \
488 LI("Elliptic curves", ec, \
489 ectab[i].name, ectab[i].name) \
490 LI("Diffie-Hellman groups", dh, \
491 ptab[i].name, ptab[i].name)
492
493 #define LIDECL(text, tag, test, name) \
494 static void show_##tag(void);
495
496 #define LIDEF(text, tag, test, name) \
497 static void show_##tag(void) \
498 { \
499 unsigned i, w; \
500 LIST(text, stdout, test, name); \
501 }
502
503 #define LIENT(text, tag, test, name) \
504 { #tag, show_##tag },
505
506 struct listent {
507 const char *name;
508 void (*list)(void);
509 };
510
511 #define MAKELISTTAB(listtab, LISTS) \
512 LISTS(LIDECL) \
513 static const struct listent listtab[] = { \
514 LISTS(LIENT) \
515 { 0, 0 } \
516 }; \
517 LISTS(LIDEF)
518
519 extern int displaylists(const struct listent */*listtab*/,
520 char *const /*argv*/[]);
521
522 /*----- Progress indicators -----------------------------------------------*/
523
524 typedef struct fprogress {
525 const char *bp;
526 off_t o, sz, olast;
527 time_t start, last;
528 char name[24];
529 } fprogress;
530
531 /* --- @fprogress_init@ --- *
532 *
533 * Arguments: @fprogress *f@ = progress context to be initialized
534 * @const char *name@ = file name string to show
535 * @FILE *fp@ = file we're reading from
536 *
537 * Returns: Zero on success, nonzero if the file's state is now broken.
538 *
539 * Use: Initializes a progress context. Nothing is actually
540 * displayed yet.
541 */
542
543 extern int fprogress_init(fprogress */*f*/,
544 const char */*name*/, FILE */*fp*/);
545
546 /* --- @fprogress_update@ --- *
547 *
548 * Arguments: @fprogress *f@ = progress context
549 * @size_t n@ = how much progress has been made
550 *
551 * Returns: ---
552 *
553 * Use: Maybe updates the display to show that some progress has been
554 * made.
555 */
556
557 extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
558
559 /* --- @fprogress_clear@ --- *
560 *
561 * Arguments: @fprogress *f@ = progress context
562 *
563 * Returns: ---
564 *
565 * Use: Clears the progress display from the screen.
566 */
567
568 extern void fprogress_clear(fprogress */*f*/);
569
570 /* --- @fprogress_done@ --- *
571 *
572 * Arguments: @fprogress *f@ = progress context
573 *
574 * Returns: ---
575 *
576 * Use: Clear up the progress context and removes any display.
577 */
578
579 extern void fprogress_done(fprogress */*f*/);
580
581 /*----- Subcommand dispatch -----------------------------------------------*/
582
583 typedef struct cmd {
584 const char *name;
585 int (*cmd)(int /*argc*/, char */*argv*/[]);
586 const char *usage;
587 const char *help;
588 } cmd;
589
590 extern void version(FILE */*fp*/);
591 extern void help_global(FILE */*fp*/);
592
593 /* --- @findcmd@ --- *
594 *
595 * Arguments: @const cmd *cmds@ = pointer to command table
596 * @const char *name@ = a command name
597 *
598 * Returns: Pointer to the command structure.
599 *
600 * Use: Looks up a command by name. If the command isn't found, an
601 * error is reported and the program is terminated.
602 */
603
604 const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
605
606 /* --- @sc_help@ --- *
607 *
608 * Arguments: @const cmd *cmds@ = pointer to command table
609 * @FILE *fp@ = output file handle
610 * @char *const *argv@ = remaining arguments
611 *
612 * Returns: ---
613 *
614 * Use: Prints a help message, maybe with help about subcommands.
615 */
616
617 extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
618 char *const */*argv*/);
619
620 /*----- That's all, folks -------------------------------------------------*/
621
622 #ifdef __cplusplus
623 }
624 #endif
625
626 #endif