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>
57 /*----- Cryptographic object tables ---------------------------------------*/
60 const struct bulkops
*ops
;
63 typedef struct bulkops
{
64 bulk
*(*init
)(key */
*k*/
, const char */
*calg*/
, const char */
*halg*/
);
65 int (*setup
)(bulk */
*b*/
, gcipher */
*cx*/
);
66 size_t (*overhead
)(bulk */
*b*/
);
67 const char *(*doit
)(bulk */
*b*/
, uint32
/*seq*/, buf */
*bb*/
,
68 const void */
*p*/
, size_t /*sz*/);
69 void (*destroy
)(bulk */
*b*/
);
74 const bulkops
*encops
;
75 const bulkops
*decops
;
78 extern const struct bulktab bulktab
[];
80 /* --- Key encapsulation --- */
83 const struct kemops
*ops
;
91 typedef struct kemops
{
92 const key_fetchdef
*kf
; /* Key fetching structure */
93 size_t kdsz
; /* Size of the key-data structure */
94 kem
*(*init
)(key */
*k*/
, void */
*kd*/
);
95 int (*doit
)(kem */
*k*/
, dstr */
*d*/
, ghash */
*h*/
);
96 const char *(*check
)(kem */
*k*/
);
97 void (*destroy
)(kem */
*k*/
);
102 const kemops
*encops
;
103 const kemops
*decops
;
106 extern const struct kemtab kemtab
[];
108 /* --- @getkem@ --- *
110 * Arguments: @key *k@ = the key to load
111 * @const char *app@ = application name
112 * @int wantpriv@ = nonzero if we want to decrypt
113 * @bulk **bc@ = bulk crypto context to set up
115 * Returns: A key-encapsulating thing.
120 extern kem
*getkem(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/,
123 /* --- @setupkem@ --- *
125 * Arguments: @kem *k@ = key-encapsulation thing
126 * @dstr *d@ = key-encapsulation data
127 * @bulk *bc@ = bulk crypto context to set up
129 * Returns: Zero for success, nonzero on faliure.
131 * Use: Initializes all the various symmetric things from a KEM.
134 extern int setupkem(kem */
*k*/
, dstr */
*d*/
, bulk */
*bc*/
);
136 /* --- @freekem@ --- *
138 * Arguments: @kem *k@ = key-encapsulation thing
142 * Use: Frees up a key-encapsulation thing.
145 extern void freekem(kem */
*k*/
);
147 /* --- Signing --- */
150 const struct sigops
*ops
;
157 typedef struct sigops
{
158 const key_fetchdef
*kf
; /* Key fetching structure */
159 size_t kdsz
; /* Size of the key-data structure */
160 sig
*(*init
)(key */
*k*/
, void */
*kd*/
, const gchash */
*hc*/
);
161 int (*doit
)(sig */
*s*/
, dstr */
*d*/
);
162 const char *(*check
)(sig */
*s*/
);
163 void (*destroy
)(sig */
*s*/
);
168 const sigops
*signops
;
169 const sigops
*verifyops
;
173 extern const struct sigtab sigtab
[];
175 /* --- @getsig@ --- *
177 * Arguments: @key *k@ = the key to load
178 * @const char *app@ = application name
179 * @int wantpriv@ = nonzero if we want to sign
181 * Returns: A signature-making thing.
183 * Use: Loads a key and starts hashing.
186 extern sig
*getsig(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
188 /* --- @freesig@ --- *
190 * Arguments: @sig *s@ = signature-making thing
194 * Use: Frees up a signature-making thing
197 extern void freesig(sig */
*s*/
);
199 /*----- File encodings ----------------------------------------------------*/
201 /* --- Data encoding --- */
204 const struct encops
*ops
;
208 typedef struct encops
{
210 const char *rmode
, *wmode
;
212 enc
*(*initenc
)(FILE */
*fp*/
, const char */
*msg*/
);
213 enc
*(*initdec
)(FILE */
*fp*/
,
214 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
215 int (*read
)(enc */
*e*/
, void */
*p*/
, size_t /*sz*/);
216 int (*write
)(enc */
*e*/
, const void */
*p*/
, size_t /*sz*/);
217 int (*encdone
)(enc */
*e*/
);
218 int (*decdone
)(enc */
*e*/
);
219 void (*destroy
)(enc */
*e*/
);
222 extern const encops enctab
[];
224 /* --- @getenc@ --- *
226 * Arguments: @const char *enc@ = name of wanted encoding
228 * Returns: Pointer to encoder operations.
230 * Use: Finds a named encoder or decoder.
233 extern const encops
*getenc(const char */
*enc*/
);
235 /* --- @checkbdry@ --- *
237 * Arguments: @const char *b@ = boundary string found
238 * @void *p@ = boundary string wanted
240 * Returns: Nonzero if the boundary string is the one we wanted.
242 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
245 extern int checkbdry(const char */
*b*/
, void */
*p*/
);
247 /* --- @initenc@ --- *
249 * Arguments: @const encops *eo@ = operations (from @getenc@)
250 * @FILE *fp@ = file handle to attach
251 * @const char *msg@ = banner message
253 * Returns: The encoder object.
255 * Use: Initializes an encoder.
258 extern enc
*initenc(const encops */
*eo*/
, FILE */
*fp*/
, const char */
*msg*/
);
260 /* --- @initdec@ --- *
262 * Arguments: @const encops *eo@ = operations (from @getenc@)
263 * @FILE *fp@ = file handle to attach
264 * @int (*func)(const char *, void *)@ = banner check function
265 * @void *p@ = argument for @func@
267 * Returns: The encoder object.
269 * Use: Initializes an encoder.
272 extern enc
*initdec(const encops */
*eo*/
, FILE */
*fp*/
,
273 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
275 /* --- @freeenc@ --- *
277 * Arguments: @enc *e@ = encoder object
281 * Use: Frees an encoder object.
284 extern void freeenc(enc */
*e*/
);
286 /* --- @cmd_encode@, @cmd_decode@ --- */
288 #define CMD_ENCODE { \
289 "encode", cmd_encode, \
290 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
294 -f, --format=FORMAT Encode to FORMAT.\n\
295 -b, --boundary=LABEL PEM boundary is LABEL.\n\
296 -o, --output=FILE Write output to FILE.\n\
297 -p, --progress Show progress on large files.\n\
300 #define CMD_DECODE { \
301 "decode", cmd_decode, \
302 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
306 -f, --format=FORMAT Decode from FORMAT.\n\
307 -b, --boundary=LABEL PEM boundary is LABEL.\n\
308 -o, --output=FILE Write output to FILE.\n\
309 -p, --progress Show progress on large files.\n\
312 extern int cmd_encode(int /*argc*/, char */
*argv*/
[]);
313 extern int cmd_decode(int /*argc*/, char */
*argv*/
[]);
315 /*----- Hash encoding functions -------------------------------------------*/
319 #define ENCODINGS(_) \
325 #define ENUM(tag, name) ENC_##tag,
331 typedef struct encodeops
{
333 void (*put
)(const octet
*, size_t, FILE *);
334 size_t (*get
)(const char *, octet
*, size_t, char **);
337 extern const encodeops encodingtab
[];
339 /* --- @getencoding@ --- *
341 * Arguments: @const char *ename@ = encoding name
343 * Returns: Pointer to encoding table entry, or null.
345 * Use: Finds an encoding entry given its name.
348 extern const encodeops
*getencoding(const char */
*ename*/
);
350 /*----- File hashing ------------------------------------------------------*/
352 typedef struct fhashstate
{
358 #define FHF_BINARY 0x100u
359 #define FHF_PROGRESS 0x200u
360 #define FHF_JUNK 0x400u
362 #define FHF_MASK 0xff00u
364 /* --- @gethash@ --- *
366 * Arguments: @const char *name@ = pointer to name string
368 * Returns: Pointer to appropriate hash class.
370 * Use: Chooses a hash function by name.
373 extern const gchash
*gethash(const char */
*name*/
);
375 /* --- @describefile@ --- *
377 * Arguments: @const struct stat *st@ = pointer to file state
379 * Returns: A snappy one-word description of the file.
382 extern const char *describefile(const struct stat */
*st*/
);
384 /* --- @fhash_init@ ---*
386 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
387 * @const gchash *gch@ = hash class to set
388 * @unsigned f@ initial flags to set
392 * Use: Initializes an @fhashstate@ structure.
395 extern void fhash_init(fhashstate */
*fh*/
,
396 const gchash */
*gch*/
, unsigned /*f*/);
398 /* --- @fhash_free@ --- *
400 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
404 * Use: Frees an fhash state.
407 extern void fhash_free(fhashstate */
*fh*/
);
411 * Arguments: @fhashstate *fh@ = pointer to fhash state
412 * @const char *file@ = file name to be hashed (null for stdin)
413 * @void *buf@ = pointer to hash output buffer
415 * Returns: Zero if it worked, nonzero on error.
417 * Use: Hashes a file.
420 extern int fhash(fhashstate */
*fh*/
, const char */
*file*/
, void */
*buf*/
);
422 /* --- @fhash_junk@ --- *
424 * Arguments: @fhashstate *fh@ = pointer to fhash state
425 * @void (*func)(const char *, const struct stat *, void *)@
426 * @void *p@ = pointer to pass to function
428 * Returns: Positive if any junk was found, negative on error, zero if
429 * everything was fine.
431 * Use: Reports junk files in any directories covered by the hash
435 extern int fhash_junk(fhashstate */
*fh*/
,
436 int (*/
*func*/
)(const char *,
441 /* --- @hfparse@ --- *
443 * Arguments: @hfpctx *hfp@ = pointer to the context structure
445 * Returns: A code indicating what happened.
447 * Use: Parses a line from the input file.
450 enum { /* Meaning and members set */
451 HF_FILE
, /* File hash: @dline@ and @hbuf@ */
452 HF_ENC
, /* Encoding: @ee@ */
453 HF_HASH
, /* Hash function: @gch@ */
454 HF_ESC
, /* Name escape: @f@ */
455 HF_EOF
, /* End of file */
456 HF_BAD
/* Unrecognized line */
459 typedef struct hfpctx
{
460 unsigned f
; /* Flags to read */
461 #define HFF_ESCAPE 1u /* File names are escaped */
462 FILE *fp
; /* Input file to read */
463 dstr
*dline
; /* Line contents, corrupted */
464 const gchash
*gch
; /* Hash function to use */
465 const encodeops
*ee
; /* Encoding to apply to hashes */
466 dstr
*dfile
; /* File name for @HF_FILE@ lines */
467 octet
*hbuf
; /* Output buffer for hash data */
470 extern int hfparse(hfpctx */
*hfp*/
);
472 /*----- String I/O --------------------------------------------------------*/
474 #define GSF_RAW 4096u
476 #define GSF_STRING 8192u
478 #define GSF_MASK 61440u
480 /* --- @getstring@ --- *
482 * Arguments: @void *in@ = input source
483 * @dstr *d@ = destination string
484 * @unsigned f@ = input flags
486 * Returns: Zero if OK, nonzero on end-of-file.
488 * Use: Reads a filename (or something similar) from a stream.
491 extern int getstring(void */
*in*/
, dstr */
*d*/
, unsigned /*f*/);
493 /* --- @putstring@ --- *
495 * Arguments: @FILE *fp@ = stream to write on
496 * @const char *p@ = pointer to text
497 * @unsigned f@ = output flags
501 * Use: Emits a string to a stream.
504 extern void putstring(FILE */
*fp*/
, const char */
*p*/
, unsigned /*f*/);
506 /*----- Lists of things ---------------------------------------------------*/
508 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
510 * Produce list of things. Requires @i@ and @w@ variables in scope.
511 * END-TEST and NAME-EXPR are in terms of @i@.
514 #define LIST(what, fp, end, name) do { \
515 fputs(what ":\n ", fp); \
517 for (i = 0; end; i++) { \
521 if (strlen(name) + w > 76) { \
523 w = 2 + strlen(name); \
526 w += strlen(name) + 1; \
534 #define STDLISTS(LI) \
535 LI("Hash functions", hash, \
536 ghashtab[i], ghashtab[i]->name) \
537 LI("Encryption schemes", enc, \
538 gciphertab[i], gciphertab[i]->name) \
539 LI("Message authentication schemes", mac, \
540 gmactab[i], gmactab[i]->name) \
541 LI("Elliptic curves", ec, \
542 ectab[i].name, ectab[i].name) \
543 LI("Diffie-Hellman groups", dh, \
544 ptab[i].name, ptab[i].name)
546 #define LIDECL(text, tag, test, name) \
547 static void show_##tag(void);
549 #define LIDEF(text, tag, test, name) \
550 static void show_##tag(void) \
553 LIST(text, stdout, test, name); \
556 #define LIENT(text, tag, test, name) \
557 { #tag, show_##tag },
564 #define MAKELISTTAB(listtab, LISTS) \
566 static const struct listent listtab[] = { \
572 extern int displaylists(const struct listent */
*listtab*/
,
573 char *const /*argv*/[]);
575 /*----- Progress indicators -----------------------------------------------*/
577 typedef struct fprogress
{
584 /* --- @fprogress_init@ --- *
586 * Arguments: @fprogress *f@ = progress context to be initialized
587 * @const char *name@ = file name string to show
588 * @FILE *fp@ = file we're reading from
590 * Returns: Zero on success, nonzero if the file's state is now broken.
592 * Use: Initializes a progress context. Nothing is actually
596 extern int fprogress_init(fprogress */
*f*/
,
597 const char */
*name*/
, FILE */
*fp*/
);
599 /* --- @fprogress_update@ --- *
601 * Arguments: @fprogress *f@ = progress context
602 * @size_t n@ = how much progress has been made
606 * Use: Maybe updates the display to show that some progress has been
610 extern void fprogress_update(fprogress */
*f*/
, size_t /*n*/);
612 /* --- @fprogress_clear@ --- *
614 * Arguments: @fprogress *f@ = progress context
618 * Use: Clears the progress display from the screen.
621 extern void fprogress_clear(fprogress */
*f*/
);
623 /* --- @fprogress_done@ --- *
625 * Arguments: @fprogress *f@ = progress context
629 * Use: Clear up the progress context and removes any display.
632 extern void fprogress_done(fprogress */
*f*/
);
634 /*----- Subcommand dispatch -----------------------------------------------*/
638 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
643 extern void version(FILE */
*fp*/
);
644 extern void help_global(FILE */
*fp*/
);
646 /* --- @findcmd@ --- *
648 * Arguments: @const cmd *cmds@ = pointer to command table
649 * @const char *name@ = a command name
651 * Returns: Pointer to the command structure.
653 * Use: Looks up a command by name. If the command isn't found, an
654 * error is reported and the program is terminated.
657 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
659 /* --- @sc_help@ --- *
661 * Arguments: @const cmd *cmds@ = pointer to command table
662 * @FILE *fp@ = output file handle
663 * @char *const *argv@ = remaining arguments
667 * Use: Prints a help message, maybe with help about subcommands.
670 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
671 char *const */
*argv*/
);
673 /*----- That's all, folks -------------------------------------------------*/