5 * Catcrypt common stuff
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
37 /*----- Header files ------------------------------------------------------*/
39 #if _FILE_OFFSET_BITS != 64
40 # error "Must set _FILE_OFFSET_BITS to 64."
47 #include <sys/types.h>
50 #include <mLib/dstr.h>
57 /*----- Cryptographic object tables ---------------------------------------*/
59 /* --- Key encapsulation --- */
62 const struct kemops *ops;
66 const gccipher *c, *cx;
70 typedef struct kemops {
71 const key_fetchdef *kf; /* Key fetching structure */
72 size_t kdsz; /* Size of the key-data structure */
73 kem *(*init)(key */*k*/, void */*kd*/);
74 int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/);
75 const char *(*check)(kem */*k*/);
76 void (*destroy)(kem */*k*/);
85 extern const struct kemtab kemtab[];
89 * Arguments: @key *k@ = the key to load
90 * @const char *app@ = application name
91 * @int wantpriv@ = nonzero if we want to decrypt
93 * Returns: A key-encapsulating thing.
98 extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/);
100 /* --- @setupkem@ --- *
102 * Arguments: @kem *k@ = key-encapsulation thing
103 * @dstr *d@ = key-encapsulation data
104 * @gcipher **cx@ = key-expansion function (for IVs)
105 * @gcipher **c@ = where to put initialized encryption scheme
106 * @gmac **m@ = where to put initialized MAC
108 * Returns: Zero for success, nonzero on faliure.
110 * Use: Initializes all the various symmetric things from a KEM.
113 extern int setupkem(kem */*k*/, dstr */*d*/,
114 gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/);
116 /* --- @freekem@ --- *
118 * Arguments: @kem *k@ = key-encapsulation thing
122 * Use: Frees up a key-encapsulation thing.
125 extern void freekem(kem */*k*/);
127 /* --- Signing --- */
130 const struct sigops *ops;
137 typedef struct sigops {
138 const key_fetchdef *kf; /* Key fetching structure */
139 size_t kdsz; /* Size of the key-data structure */
140 sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/);
141 int (*doit)(sig */*s*/, dstr */*d*/);
142 const char *(*check)(sig */*s*/);
143 void (*destroy)(sig */*s*/);
148 const sigops *signops;
149 const sigops *verifyops;
153 extern const struct sigtab sigtab[];
155 /* --- @getsig@ --- *
157 * Arguments: @key *k@ = the key to load
158 * @const char *app@ = application name
159 * @int wantpriv@ = nonzero if we want to sign
161 * Returns: A signature-making thing.
163 * Use: Loads a key and starts hashing.
166 extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/);
168 /* --- @freesig@ --- *
170 * Arguments: @sig *s@ = signature-making thing
174 * Use: Frees up a signature-making thing
177 extern void freesig(sig */*s*/);
179 /*----- File encodings ----------------------------------------------------*/
181 /* --- Data encoding --- */
184 const struct encops *ops;
188 typedef struct encops {
190 const char *rmode, *wmode;
192 enc *(*initenc)(FILE */*fp*/, const char */*msg*/);
193 enc *(*initdec)(FILE */*fp*/,
194 int (*/*func*/)(const char *, void *), void */*p*/);
195 int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/);
196 int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/);
197 int (*encdone)(enc */*e*/);
198 int (*decdone)(enc */*e*/);
199 void (*destroy)(enc */*e*/);
202 extern const encops enctab[];
204 /* --- @getenc@ --- *
206 * Arguments: @const char *enc@ = name of wanted encoding
208 * Returns: Pointer to encoder operations.
210 * Use: Finds a named encoder or decoder.
213 extern const encops *getenc(const char */*enc*/);
215 /* --- @checkbdry@ --- *
217 * Arguments: @const char *b@ = boundary string found
218 * @void *p@ = boundary string wanted
220 * Returns: Nonzero if the boundary string is the one we wanted.
222 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
225 extern int checkbdry(const char */*b*/, void */*p*/);
227 /* --- @initenc@ --- *
229 * Arguments: @const encops *eo@ = operations (from @getenc@)
230 * @FILE *fp@ = file handle to attach
231 * @const char *msg@ = banner message
233 * Returns: The encoder object.
235 * Use: Initializes an encoder.
238 extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
240 /* --- @initdec@ --- *
242 * Arguments: @const encops *eo@ = operations (from @getenc@)
243 * @FILE *fp@ = file handle to attach
244 * @int (*func)(const char *, void *)@ = banner check function
245 * @void *p@ = argument for @func@
247 * Returns: The encoder object.
249 * Use: Initializes an encoder.
252 extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
253 int (*/*func*/)(const char *, void *), void */*p*/);
255 /* --- @freeenc@ --- *
257 * Arguments: @enc *e@ = encoder object
261 * Use: Frees an encoder object.
264 extern void freeenc(enc */*e*/);
266 /* --- @cmd_encode@, @cmd_decode@ --- */
268 #define CMD_ENCODE { \
269 "encode", cmd_encode, \
270 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
274 -f, --format=FORMAT Encode to FORMAT.\n\
275 -b, --boundary=LABEL PEM boundary is LABEL.\n\
276 -o, --output=FILE Write output to FILE.\n\
277 -p, --progress Show progress on large files.\n\
280 #define CMD_DECODE { \
281 "decode", cmd_decode, \
282 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
286 -f, --format=FORMAT Decode from FORMAT.\n\
287 -b, --boundary=LABEL PEM boundary is LABEL.\n\
288 -o, --output=FILE Write output to FILE.\n\
289 -p, --progress Show progress on large files.\n\
292 extern int cmd_encode(int /*argc*/, char */*argv*/[]);
293 extern int cmd_decode(int /*argc*/, char */*argv*/[]);
295 /*----- Hash encoding functions -------------------------------------------*/
299 #define ENCODINGS(_) \
305 #define ENUM(tag, name) ENC_##tag,
311 typedef struct encodeops {
313 void (*put)(const octet *, size_t, FILE *);
314 size_t (*get)(const char *, octet *, size_t, char **);
317 extern const encodeops encodingtab[];
319 /* --- @getencoding@ --- *
321 * Arguments: @const char *ename@ = encoding name
323 * Returns: Pointer to encoding table entry, or null.
325 * Use: Finds an encoding entry given its name.
328 extern const encodeops *getencoding(const char */*ename*/);
330 /*----- File hashing ------------------------------------------------------*/
332 typedef struct fhashstate {
338 #define FHF_BINARY 0x100u
339 #define FHF_PROGRESS 0x200u
340 #define FHF_JUNK 0x400u
342 #define FHF_MASK 0xff00u
344 /* --- @gethash@ --- *
346 * Arguments: @const char *name@ = pointer to name string
348 * Returns: Pointer to appropriate hash class.
350 * Use: Chooses a hash function by name.
353 extern const gchash *gethash(const char */*name*/);
355 /* --- @describefile@ --- *
357 * Arguments: @const struct stat *st@ = pointer to file state
359 * Returns: A snappy one-word description of the file.
362 extern const char *describefile(const struct stat */*st*/);
364 /* --- @fhash_init@ ---*
366 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
367 * @const gchash *gch@ = hash class to set
368 * @unsigned f@ initial flags to set
372 * Use: Initializes an @fhashstate@ structure.
375 extern void fhash_init(fhashstate */*fh*/,
376 const gchash */*gch*/, unsigned /*f*/);
378 /* --- @fhash_free@ --- *
380 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
384 * Use: Frees an fhash state.
387 extern void fhash_free(fhashstate */*fh*/);
391 * Arguments: @fhashstate *fh@ = pointer to fhash state
392 * @const char *file@ = file name to be hashed (null for stdin)
393 * @void *buf@ = pointer to hash output buffer
395 * Returns: Zero if it worked, nonzero on error.
397 * Use: Hashes a file.
400 extern int fhash(fhashstate */*fh*/, const char */*file*/, void */*buf*/);
402 /* --- @fhash_junk@ --- *
404 * Arguments: @fhashstate *fh@ = pointer to fhash state
405 * @void (*func)(const char *, const struct stat *, void *)@
406 * @void *p@ = pointer to pass to function
408 * Returns: Positive if any junk was found, negative on error, zero if
409 * everything was fine.
411 * Use: Reports junk files in any directories covered by the hash
415 extern int fhash_junk(fhashstate */*fh*/,
416 int (*/*func*/)(const char *,
421 /* --- @hfparse@ --- *
423 * Arguments: @hfpctx *hfp@ = pointer to the context structure
425 * Returns: A code indicating what happened.
427 * Use: Parses a line from the input file.
430 enum { /* Meaning and members set */
431 HF_FILE, /* File hash: @dline@ and @hbuf@ */
432 HF_ENC, /* Encoding: @ee@ */
433 HF_HASH, /* Hash function: @gch@ */
434 HF_ESC, /* Name escape: @f@ */
435 HF_EOF, /* End of file */
436 HF_BAD /* Unrecognized line */
439 typedef struct hfpctx {
440 unsigned f; /* Flags to read */
441 #define HFF_ESCAPE 1u /* File names are escaped */
442 FILE *fp; /* Input file to read */
443 dstr *dline; /* Line contents, corrupted */
444 const gchash *gch; /* Hash function to use */
445 const encodeops *ee; /* Encoding to apply to hashes */
446 dstr *dfile; /* File name for @HF_FILE@ lines */
447 octet *hbuf; /* Output buffer for hash data */
450 extern int hfparse(hfpctx */*hfp*/);
452 /*----- String I/O --------------------------------------------------------*/
454 #define GSF_RAW 4096u
456 #define GSF_STRING 8192u
458 #define GSF_MASK 61440u
460 /* --- @getstring@ --- *
462 * Arguments: @void *in@ = input source
463 * @dstr *d@ = destination string
464 * @unsigned f@ = input flags
466 * Returns: Zero if OK, nonzero on end-of-file.
468 * Use: Reads a filename (or something similar) from a stream.
471 extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/);
473 /* --- @putstring@ --- *
475 * Arguments: @FILE *fp@ = stream to write on
476 * @const char *p@ = pointer to text
477 * @unsigned f@ = output flags
481 * Use: Emits a string to a stream.
484 extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/);
486 /*----- Lists of things ---------------------------------------------------*/
488 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
490 * Produce list of things. Requires @i@ and @w@ variables in scope.
491 * END-TEST and NAME-EXPR are in terms of @i@.
494 #define LIST(what, fp, end, name) do { \
495 fputs(what ":\n ", fp); \
497 for (i = 0; end; i++) { \
501 if (strlen(name) + w > 76) { \
503 w = 2 + strlen(name); \
506 w += strlen(name) + 1; \
514 #define STDLISTS(LI) \
515 LI("Hash functions", hash, \
516 ghashtab[i], ghashtab[i]->name) \
517 LI("Encryption schemes", enc, \
518 gciphertab[i], gciphertab[i]->name) \
519 LI("Message authentication schemes", mac, \
520 gmactab[i], gmactab[i]->name) \
521 LI("Elliptic curves", ec, \
522 ectab[i].name, ectab[i].name) \
523 LI("Diffie-Hellman groups", dh, \
524 ptab[i].name, ptab[i].name)
526 #define LIDECL(text, tag, test, name) \
527 static void show_##tag(void);
529 #define LIDEF(text, tag, test, name) \
530 static void show_##tag(void) \
533 LIST(text, stdout, test, name); \
536 #define LIENT(text, tag, test, name) \
537 { #tag, show_##tag },
544 #define MAKELISTTAB(listtab, LISTS) \
546 static const struct listent listtab[] = { \
552 extern int displaylists(const struct listent */*listtab*/,
553 char *const /*argv*/[]);
555 /*----- Progress indicators -----------------------------------------------*/
557 typedef struct fprogress {
564 /* --- @fprogress_init@ --- *
566 * Arguments: @fprogress *f@ = progress context to be initialized
567 * @const char *name@ = file name string to show
568 * @FILE *fp@ = file we're reading from
570 * Returns: Zero on success, nonzero if the file's state is now broken.
572 * Use: Initializes a progress context. Nothing is actually
576 extern int fprogress_init(fprogress */*f*/,
577 const char */*name*/, FILE */*fp*/);
579 /* --- @fprogress_update@ --- *
581 * Arguments: @fprogress *f@ = progress context
582 * @size_t n@ = how much progress has been made
586 * Use: Maybe updates the display to show that some progress has been
590 extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
592 /* --- @fprogress_clear@ --- *
594 * Arguments: @fprogress *f@ = progress context
598 * Use: Clears the progress display from the screen.
601 extern void fprogress_clear(fprogress */*f*/);
603 /* --- @fprogress_done@ --- *
605 * Arguments: @fprogress *f@ = progress context
609 * Use: Clear up the progress context and removes any display.
612 extern void fprogress_done(fprogress */*f*/);
614 /*----- Subcommand dispatch -----------------------------------------------*/
618 int (*cmd)(int /*argc*/, char */*argv*/[]);
623 extern void version(FILE */*fp*/);
624 extern void help_global(FILE */*fp*/);
626 /* --- @findcmd@ --- *
628 * Arguments: @const cmd *cmds@ = pointer to command table
629 * @const char *name@ = a command name
631 * Returns: Pointer to the command structure.
633 * Use: Looks up a command by name. If the command isn't found, an
634 * error is reported and the program is terminated.
637 const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
639 /* --- @sc_help@ --- *
641 * Arguments: @const cmd *cmds@ = pointer to command table
642 * @FILE *fp@ = output file handle
643 * @char *const *argv@ = remaining arguments
647 * Use: Prints a help message, maybe with help about subcommands.
650 extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
651 char *const */*argv*/);
653 /*----- That's all, folks -------------------------------------------------*/