3 * Catcrypt common stuff
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /*----- Header files ------------------------------------------------------*/
37 #if _FILE_OFFSET_BITS != 64
38 # error "Must set _FILE_OFFSET_BITS to 64."
45 #include <sys/types.h>
48 #include <mLib/dstr.h>
55 /*----- Cryptographic object tables ---------------------------------------*/
57 /* --- Key encapsulation --- */
60 const struct kemops *ops;
64 const gccipher *c, *cx;
68 typedef struct kemops {
69 const key_fetchdef *kf; /* Key fetching structure */
70 size_t kdsz; /* Size of the key-data structure */
71 kem *(*init)(key */*k*/, void */*kd*/);
72 int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/);
73 const char *(*check)(kem */*k*/);
74 void (*destroy)(kem */*k*/);
83 extern const struct kemtab kemtab[];
87 * Arguments: @key *k@ = the key to load
88 * @const char *app@ = application name
89 * @int wantpriv@ = nonzero if we want to decrypt
91 * Returns: A key-encapsulating thing.
96 extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/);
98 /* --- @setupkem@ --- *
100 * Arguments: @kem *k@ = key-encapsulation thing
101 * @dstr *d@ = key-encapsulation data
102 * @gcipher **cx@ = key-expansion function (for IVs)
103 * @gcipher **c@ = where to put initialized encryption scheme
104 * @gmac **m@ = where to put initialized MAC
106 * Returns: Zero for success, nonzero on faliure.
108 * Use: Initializes all the various symmetric things from a KEM.
111 extern int setupkem(kem */*k*/, dstr */*d*/,
112 gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/);
114 /* --- @freekem@ --- *
116 * Arguments: @kem *k@ = key-encapsulation thing
120 * Use: Frees up a key-encapsulation thing.
123 extern void freekem(kem */*k*/);
125 /* --- Signing --- */
128 const struct sigops *ops;
135 typedef struct sigops {
136 const key_fetchdef *kf; /* Key fetching structure */
137 size_t kdsz; /* Size of the key-data structure */
138 sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/);
139 int (*doit)(sig */*s*/, dstr */*d*/);
140 const char *(*check)(sig */*s*/);
141 void (*destroy)(sig */*s*/);
146 const sigops *signops;
147 const sigops *verifyops;
151 extern const struct sigtab sigtab[];
153 /* --- @getsig@ --- *
155 * Arguments: @key *k@ = the key to load
156 * @const char *app@ = application name
157 * @int wantpriv@ = nonzero if we want to sign
159 * Returns: A signature-making thing.
161 * Use: Loads a key and starts hashing.
164 extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/);
166 /* --- @freesig@ --- *
168 * Arguments: @sig *s@ = signature-making thing
172 * Use: Frees up a signature-making thing
175 extern void freesig(sig */*s*/);
177 /*----- File encodings ----------------------------------------------------*/
179 /* --- Data encoding --- */
182 const struct encops *ops;
186 typedef struct encops {
188 const char *rmode, *wmode;
190 enc *(*initenc)(FILE */*fp*/, const char */*msg*/);
191 enc *(*initdec)(FILE */*fp*/,
192 int (*/*func*/)(const char *, void *), void */*p*/);
193 int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/);
194 int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/);
195 int (*encdone)(enc */*e*/);
196 int (*decdone)(enc */*e*/);
197 void (*destroy)(enc */*e*/);
200 extern const encops enctab[];
202 /* --- @getenc@ --- *
204 * Arguments: @const char *enc@ = name of wanted encoding
206 * Returns: Pointer to encoder operations.
208 * Use: Finds a named encoder or decoder.
211 extern const encops *getenc(const char */*enc*/);
213 /* --- @checkbdry@ --- *
215 * Arguments: @const char *b@ = boundary string found
216 * @void *p@ = boundary string wanted
218 * Returns: Nonzero if the boundary string is the one we wanted.
220 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
223 extern int checkbdry(const char */*b*/, void */*p*/);
225 /* --- @initenc@ --- *
227 * Arguments: @const encops *eo@ = operations (from @getenc@)
228 * @FILE *fp@ = file handle to attach
229 * @const char *msg@ = banner message
231 * Returns: The encoder object.
233 * Use: Initializes an encoder.
236 extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
238 /* --- @initdec@ --- *
240 * Arguments: @const encops *eo@ = operations (from @getenc@)
241 * @FILE *fp@ = file handle to attach
242 * @int (*func)(const char *, void *)@ = banner check function
243 * @void *p@ = argument for @func@
245 * Returns: The encoder object.
247 * Use: Initializes an encoder.
250 extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
251 int (*/*func*/)(const char *, void *), void */*p*/);
253 /* --- @freeenc@ --- *
255 * Arguments: @enc *e@ = encoder object
259 * Use: Frees an encoder object.
262 extern void freeenc(enc */*e*/);
264 /* --- @cmd_encode@, @cmd_decode@ --- */
266 #define CMD_ENCODE { \
267 "encode", cmd_encode, \
268 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
272 -f, --format=FORMAT Encode to FORMAT.\n\
273 -b, --boundary=LABEL PEM boundary is LABEL.\n\
274 -o, --output=FILE Write output to FILE.\n\
275 -p, --progress Show progress on large files.\n\
278 #define CMD_DECODE { \
279 "decode", cmd_decode, \
280 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
284 -f, --format=FORMAT Decode from FORMAT.\n\
285 -b, --boundary=LABEL PEM boundary is LABEL.\n\
286 -o, --output=FILE Write output to FILE.\n\
287 -p, --progress Show progress on large files.\n\
290 extern int cmd_encode(int /*argc*/, char */*argv*/[]);
291 extern int cmd_decode(int /*argc*/, char */*argv*/[]);
293 /*----- Hash encoding functions -------------------------------------------*/
297 #define ENCODINGS(_) \
303 #define ENUM(tag, name) ENC_##tag,
309 typedef struct encodeops {
311 void (*put)(const octet *, size_t, FILE *);
312 size_t (*get)(const char *, octet *, size_t, char **);
315 extern const encodeops encodingtab[];
317 /* --- @getencoding@ --- *
319 * Arguments: @const char *ename@ = encoding name
321 * Returns: Pointer to encoding table entry, or null.
323 * Use: Finds an encoding entry given its name.
326 extern const encodeops *getencoding(const char */*ename*/);
328 /*----- File hashing ------------------------------------------------------*/
330 typedef struct fhashstate {
336 #define FHF_BINARY 0x100u
337 #define FHF_PROGRESS 0x200u
338 #define FHF_JUNK 0x400u
340 #define FHF_MASK 0xff00u
342 /* --- @gethash@ --- *
344 * Arguments: @const char *name@ = pointer to name string
346 * Returns: Pointer to appropriate hash class.
348 * Use: Chooses a hash function by name.
351 extern const gchash *gethash(const char */*name*/);
353 /* --- @describefile@ --- *
355 * Arguments: @const struct stat *st@ = pointer to file state
357 * Returns: A snappy one-word description of the file.
360 extern const char *describefile(const struct stat */*st*/);
362 /* --- @fhash_init@ ---*
364 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
365 * @const gchash *gch@ = hash class to set
366 * @unsigned f@ initial flags to set
370 * Use: Initializes an @fhashstate@ structure.
373 extern void fhash_init(fhashstate */*fh*/,
374 const gchash */*gch*/, unsigned /*f*/);
376 /* --- @fhash_free@ --- *
378 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
382 * Use: Frees an fhash state.
385 extern void fhash_free(fhashstate */*fh*/);
389 * Arguments: @fhashstate *fh@ = pointer to fhash state
390 * @const char *file@ = file name to be hashed (null for stdin)
391 * @void *buf@ = pointer to hash output buffer
393 * Returns: Zero if it worked, nonzero on error.
395 * Use: Hashes a file.
398 extern int fhash(fhashstate */*fh*/, const char */*file*/, void */*buf*/);
400 /* --- @fhash_junk@ --- *
402 * Arguments: @fhashstate *fh@ = pointer to fhash state
403 * @void (*func)(const char *, const struct stat *, void *)@
404 * @void *p@ = pointer to pass to function
406 * Returns: Positive if any junk was found, negative on error, zero if
407 * everything was fine.
409 * Use: Reports junk files in any directories covered by the hash
413 extern int fhash_junk(fhashstate */*fh*/,
414 int (*/*func*/)(const char *,
419 /* --- @hfparse@ --- *
421 * Arguments: @hfpctx *hfp@ = pointer to the context structure
423 * Returns: A code indicating what happened.
425 * Use: Parses a line from the input file.
428 enum { /* Meaning and members set */
429 HF_FILE, /* File hash: @dline@ and @hbuf@ */
430 HF_ENC, /* Encoding: @ee@ */
431 HF_HASH, /* Hash function: @gch@ */
432 HF_ESC, /* Name escape: @f@ */
433 HF_EOF, /* End of file */
434 HF_BAD /* Unrecognized line */
437 typedef struct hfpctx {
438 unsigned f; /* Flags to read */
439 #define HFF_ESCAPE 1u /* File names are escaped */
440 FILE *fp; /* Input file to read */
441 dstr *dline; /* Line contents, corrupted */
442 const gchash *gch; /* Hash function to use */
443 const encodeops *ee; /* Encoding to apply to hashes */
444 dstr *dfile; /* File name for @HF_FILE@ lines */
445 octet *hbuf; /* Output buffer for hash data */
448 extern int hfparse(hfpctx */*hfp*/);
450 /*----- String I/O --------------------------------------------------------*/
452 #define GSF_RAW 4096u
454 #define GSF_STRING 8192u
456 #define GSF_MASK 61440u
458 /* --- @getstring@ --- *
460 * Arguments: @void *in@ = input source
461 * @dstr *d@ = destination string
462 * @unsigned f@ = input flags
464 * Returns: Zero if OK, nonzero on end-of-file.
466 * Use: Reads a filename (or something similar) from a stream.
469 extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/);
471 /* --- @putstring@ --- *
473 * Arguments: @FILE *fp@ = stream to write on
474 * @const char *p@ = pointer to text
475 * @unsigned f@ = output flags
479 * Use: Emits a string to a stream.
482 extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/);
484 /*----- Lists of things ---------------------------------------------------*/
486 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
488 * Produce list of things. Requires @i@ and @w@ variables in scope.
489 * END-TEST and NAME-EXPR are in terms of @i@.
492 #define LIST(what, fp, end, name) do { \
493 fputs(what ":\n ", fp); \
495 for (i = 0; end; i++) { \
499 if (strlen(name) + w > 76) { \
501 w = 2 + strlen(name); \
504 w += strlen(name) + 1; \
512 #define STDLISTS(LI) \
513 LI("Hash functions", hash, \
514 ghashtab[i], ghashtab[i]->name) \
515 LI("Encryption schemes", enc, \
516 gciphertab[i], gciphertab[i]->name) \
517 LI("Message authentication schemes", mac, \
518 gmactab[i], gmactab[i]->name) \
519 LI("Elliptic curves", ec, \
520 ectab[i].name, ectab[i].name) \
521 LI("Diffie-Hellman groups", dh, \
522 ptab[i].name, ptab[i].name)
524 #define LIDECL(text, tag, test, name) \
525 static void show_##tag(void);
527 #define LIDEF(text, tag, test, name) \
528 static void show_##tag(void) \
531 LIST(text, stdout, test, name); \
534 #define LIENT(text, tag, test, name) \
535 { #tag, show_##tag },
542 #define MAKELISTTAB(listtab, LISTS) \
544 static const struct listent listtab[] = { \
550 extern int displaylists(const struct listent */*listtab*/,
551 char *const /*argv*/[]);
553 /*----- Progress indicators -----------------------------------------------*/
555 typedef struct fprogress {
562 /* --- @fprogress_init@ --- *
564 * Arguments: @fprogress *f@ = progress context to be initialized
565 * @const char *name@ = file name string to show
566 * @FILE *fp@ = file we're reading from
568 * Returns: Zero on success, nonzero if the file's state is now broken.
570 * Use: Initializes a progress context. Nothing is actually
574 extern int fprogress_init(fprogress */*f*/,
575 const char */*name*/, FILE */*fp*/);
577 /* --- @fprogress_update@ --- *
579 * Arguments: @fprogress *f@ = progress context
580 * @size_t n@ = how much progress has been made
584 * Use: Maybe updates the display to show that some progress has been
588 extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
590 /* --- @fprogress_clear@ --- *
592 * Arguments: @fprogress *f@ = progress context
596 * Use: Clears the progress display from the screen.
599 extern void fprogress_clear(fprogress */*f*/);
601 /* --- @fprogress_done@ --- *
603 * Arguments: @fprogress *f@ = progress context
607 * Use: Clear up the progress context and removes any display.
610 extern void fprogress_done(fprogress */*f*/);
612 /*----- Subcommand dispatch -----------------------------------------------*/
616 int (*cmd)(int /*argc*/, char */*argv*/[]);
621 extern void version(FILE */*fp*/);
622 extern void help_global(FILE */*fp*/);
624 /* --- @findcmd@ --- *
626 * Arguments: @const cmd *cmds@ = pointer to command table
627 * @const char *name@ = a command name
629 * Returns: Pointer to the command structure.
631 * Use: Looks up a command by name. If the command isn't found, an
632 * error is reported and the program is terminated.
635 const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
637 /* --- @sc_help@ --- *
639 * Arguments: @const cmd *cmds@ = pointer to command table
640 * @FILE *fp@ = output file handle
641 * @char *const *argv@ = remaining arguments
645 * Use: Prints a help message, maybe with help about subcommands.
648 extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
649 char *const */*argv*/);
651 /*----- That's all, folks -------------------------------------------------*/