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 <mLib/dstr.h>
54 /*----- Cryptographic object tables ---------------------------------------*/
56 /* --- Key encapsulation --- */
59 const struct kemops
*ops
;
63 const gccipher
*c
, *cx
;
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*/
);
82 extern const struct kemtab kemtab
[];
86 * Arguments: @key *k@ = the key to load
87 * @const char *app@ = application name
88 * @int wantpriv@ = nonzero if we want to decrypt
90 * Returns: A key-encapsulating thing.
95 extern kem
*getkem(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
97 /* --- @setupkem@ --- *
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
105 * Returns: Zero for success, nonzero on faliure.
107 * Use: Initializes all the various symmetric things from a KEM.
110 extern int setupkem(kem */
*k*/
, dstr */
*d*/
,
111 gcipher
**/
*cx*/
, gcipher
**/
*c*/
, gmac
**/
*m*/
);
113 /* --- @freekem@ --- *
115 * Arguments: @kem *k@ = key-encapsulation thing
119 * Use: Frees up a key-encapsulation thing.
122 extern void freekem(kem */
*k*/
);
124 /* --- Signing --- */
127 const struct sigops
*ops
;
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*/
);
145 const sigops
*signops
;
146 const sigops
*verifyops
;
150 extern const struct sigtab sigtab
[];
152 /* --- @getsig@ --- *
154 * Arguments: @key *k@ = the key to load
155 * @const char *app@ = application name
156 * @int wantpriv@ = nonzero if we want to sign
158 * Returns: A signature-making thing.
160 * Use: Loads a key and starts hashing.
163 extern sig
*getsig(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
165 /* --- @freesig@ --- *
167 * Arguments: @sig *s@ = signature-making thing
171 * Use: Frees up a signature-making thing
174 extern void freesig(sig */
*s*/
);
176 /*----- File encodings ----------------------------------------------------*/
178 /* --- Data encoding --- */
181 const struct encops
*ops
;
185 typedef struct encops
{
187 const char *rmode
, *wmode
;
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*/
);
199 extern const encops enctab
[];
201 /* --- @getenc@ --- *
203 * Arguments: @const char *enc@ = name of wanted encoding
205 * Returns: Pointer to encoder operations.
207 * Use: Finds a named encoder or decoder.
210 extern const encops
*getenc(const char */
*enc*/
);
212 /* --- @checkbdry@ --- *
214 * Arguments: @const char *b@ = boundary string found
215 * @void *p@ = boundary string wanted
217 * Returns: Nonzero if the boundary string is the one we wanted.
219 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
222 extern int checkbdry(const char */
*b*/
, void */
*p*/
);
224 /* --- @initenc@ --- *
226 * Arguments: @const encops *eo@ = operations (from @getenc@)
227 * @FILE *fp@ = file handle to attach
228 * @const char *msg@ = banner message
230 * Returns: The encoder object.
232 * Use: Initializes an encoder.
235 extern enc
*initenc(const encops */
*eo*/
, FILE */
*fp*/
, const char */
*msg*/
);
237 /* --- @initdec@ --- *
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@
244 * Returns: The encoder object.
246 * Use: Initializes an encoder.
249 extern enc
*initdec(const encops */
*eo*/
, FILE */
*fp*/
,
250 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
252 /* --- @freeenc@ --- *
254 * Arguments: @enc *e@ = encoder object
258 * Use: Frees an encoder object.
261 extern void freeenc(enc */
*e*/
);
263 /* --- @cmd_encode@, @cmd_decode@ --- */
265 #define CMD_ENCODE { \
266 "encode", cmd_encode, \
267 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
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\
277 #define CMD_DECODE { \
278 "decode", cmd_decode, \
279 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
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\
289 extern int cmd_encode(int /*argc*/, char */
*argv*/
[]);
290 extern int cmd_decode(int /*argc*/, char */
*argv*/
[]);
292 /*----- Hash encoding functions -------------------------------------------*/
296 #define ENCODINGS(_) \
302 #define ENUM(tag, name) ENC_##tag,
308 typedef struct encodeops
{
310 void (*put
)(const octet
*, size_t, FILE *);
311 size_t (*get
)(const char *, octet
*, size_t, char **);
314 extern const encodeops encodingtab
[];
316 /* --- @getencoding@ --- *
318 * Arguments: @const char *ename@ = encoding name
320 * Returns: Pointer to encoding table entry, or null.
322 * Use: Finds an encoding entry given its name.
325 extern const encodeops
*getencoding(const char */
*ename*/
);
327 /*----- File hashing ------------------------------------------------------*/
329 typedef struct fhashstate
{
334 #define FHF_BINARY 0x100u
335 #define FHF_PROGRESS 0x200u
337 #define FHF_MASK 0xff00u
339 /* --- @gethash@ --- *
341 * Arguments: @const char *name@ = pointer to name string
343 * Returns: Pointer to appropriate hash class.
345 * Use: Chooses a hash function by name.
348 extern const gchash
*gethash(const char */
*name*/
);
350 /* --- @fhash_init@ ---*
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
358 * Use: Initializes an @fhashstate@ structure.
361 extern void fhash_init(fhashstate */
*fh*/
,
362 const gchash */
*gch*/
, unsigned /*f*/);
364 /* --- @fhash_free@ --- *
366 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
370 * Use: Frees an fhash state.
373 extern void fhash_free(fhashstate */
*fh*/
);
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
381 * Returns: Zero if it worked, nonzero on error.
383 * Use: Hashes a file.
386 extern int fhash(fhashstate */
*fh*/
, const char */
*file*/
, void */
*buf*/
);
388 /* --- @hfparse@ --- *
390 * Arguments: @hfpctx *hfp@ = pointer to the context structure
392 * Returns: A code indicating what happened.
394 * Use: Parses a line from the input file.
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 */
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 */
417 extern int hfparse(hfpctx */
*hfp*/
);
419 /*----- String I/O --------------------------------------------------------*/
421 #define GSF_RAW 4096u
423 #define GSF_STRING 8192u
425 #define GSF_MASK 61440u
427 /* --- @getstring@ --- *
429 * Arguments: @void *in@ = input source
430 * @dstr *d@ = destination string
431 * @unsigned f@ = input flags
433 * Returns: Zero if OK, nonzero on end-of-file.
435 * Use: Reads a filename (or something similar) from a stream.
438 extern int getstring(void */
*in*/
, dstr */
*d*/
, unsigned /*f*/);
440 /* --- @putstring@ --- *
442 * Arguments: @FILE *fp@ = stream to write on
443 * @const char *p@ = pointer to text
444 * @unsigned f@ = output flags
448 * Use: Emits a string to a stream.
451 extern void putstring(FILE */
*fp*/
, const char */
*p*/
, unsigned /*f*/);
453 /*----- Lists of things ---------------------------------------------------*/
455 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
457 * Produce list of things. Requires @i@ and @w@ variables in scope.
458 * END-TEST and NAME-EXPR are in terms of @i@.
461 #define LIST(what, fp, end, name) do { \
462 fputs(what ":\n ", fp); \
464 for (i = 0; end; i++) { \
468 if (strlen(name) + w > 76) { \
470 w = 2 + strlen(name); \
473 w += strlen(name) + 1; \
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)
493 #define LIDECL(text, tag, test, name) \
494 static void show_##tag(void);
496 #define LIDEF(text, tag, test, name) \
497 static void show_##tag(void) \
500 LIST(text, stdout, test, name); \
503 #define LIENT(text, tag, test, name) \
504 { #tag, show_##tag },
511 #define MAKELISTTAB(listtab, LISTS) \
513 static const struct listent listtab[] = { \
519 extern int displaylists(const struct listent */
*listtab*/
,
520 char *const /*argv*/[]);
522 /*----- Progress indicators -----------------------------------------------*/
524 typedef struct fprogress
{
531 /* --- @fprogress_init@ --- *
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
537 * Returns: Zero on success, nonzero if the file's state is now broken.
539 * Use: Initializes a progress context. Nothing is actually
543 extern int fprogress_init(fprogress */
*f*/
,
544 const char */
*name*/
, FILE */
*fp*/
);
546 /* --- @fprogress_update@ --- *
548 * Arguments: @fprogress *f@ = progress context
549 * @size_t n@ = how much progress has been made
553 * Use: Maybe updates the display to show that some progress has been
557 extern void fprogress_update(fprogress */
*f*/
, size_t /*n*/);
559 /* --- @fprogress_clear@ --- *
561 * Arguments: @fprogress *f@ = progress context
565 * Use: Clears the progress display from the screen.
568 extern void fprogress_clear(fprogress */
*f*/
);
570 /* --- @fprogress_done@ --- *
572 * Arguments: @fprogress *f@ = progress context
576 * Use: Clear up the progress context and removes any display.
579 extern void fprogress_done(fprogress */
*f*/
);
581 /*----- Subcommand dispatch -----------------------------------------------*/
585 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
590 extern void version(FILE */
*fp*/
);
591 extern void help_global(FILE */
*fp*/
);
593 /* --- @findcmd@ --- *
595 * Arguments: @const cmd *cmds@ = pointer to command table
596 * @const char *name@ = a command name
598 * Returns: Pointer to the command structure.
600 * Use: Looks up a command by name. If the command isn't found, an
601 * error is reported and the program is terminated.
604 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
606 /* --- @sc_help@ --- *
608 * Arguments: @const cmd *cmds@ = pointer to command table
609 * @FILE *fp@ = output file handle
610 * @char *const *argv@ = remaining arguments
614 * Use: Prints a help message, maybe with help about subcommands.
617 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
618 char *const */
*argv*/
);
620 /*----- That's all, folks -------------------------------------------------*/