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 /* --- @gethash@ --- *
331 * Arguments: @const char *name@ = pointer to name string
333 * Returns: Pointer to appropriate hash class.
335 * Use: Chooses a hash function by name.
338 extern const gchash
*gethash(const char */
*name*/
);
342 * Arguments: @const gchash *gch@ = pointer to hash function to use
343 * @unsigned f@ = flags to set
344 * @const char *file@ = file name to be hashed (null for stdin)
345 * @void *buf@ = pointer to hash output buffer
347 * Returns: Zero if it worked, nonzero on error.
349 * Use: Hashes a file.
352 #define FHF_BINARY 256u
353 #define FHF_PROGRESS 512u
355 #define FHF_MASK 384u
357 extern int fhash(const gchash */
*gch*/
, unsigned /*f*/,
358 const char */
*file*/
, void */
*buf*/
);
360 /* --- @hfparse@ --- *
362 * Arguments: @hfpctx *hfp@ = pointer to the context structure
364 * Returns: A code indicating what happened.
366 * Use: Parses a line from the input file.
369 enum { /* Meaning and members set */
370 HF_FILE
, /* File hash: @dline@ and @hbuf@ */
371 HF_ENC
, /* Encoding: @ee@ */
372 HF_HASH
, /* Hash function: @gch@ */
373 HF_ESC
, /* Name escape: @f@ */
374 HF_EOF
, /* End of file */
375 HF_BAD
/* Unrecognized line */
378 typedef struct hfpctx
{
379 unsigned f
; /* Flags to read */
380 #define HFF_ESCAPE 1u /* File names are escaped */
381 FILE *fp
; /* Input file to read */
382 dstr
*dline
; /* Line contents, corrupted */
383 const gchash
*gch
; /* Hash function to use */
384 const encodeops
*ee
; /* Encoding to apply to hashes */
385 dstr
*dfile
; /* File name for @HF_FILE@ lines */
386 octet
*hbuf
; /* Output buffer for hash data */
389 extern int hfparse(hfpctx */
*hfp*/
);
391 /*----- String I/O --------------------------------------------------------*/
393 #define GSF_RAW 4096u
395 #define GSF_STRING 8192u
397 #define GSF_MASK 61440u
399 /* --- @getstring@ --- *
401 * Arguments: @void *in@ = input source
402 * @dstr *d@ = destination string
403 * @unsigned f@ = input flags
405 * Returns: Zero if OK, nonzero on end-of-file.
407 * Use: Reads a filename (or something similar) from a stream.
410 extern int getstring(void */
*in*/
, dstr */
*d*/
, unsigned /*f*/);
412 /* --- @putstring@ --- *
414 * Arguments: @FILE *fp@ = stream to write on
415 * @const char *p@ = pointer to text
416 * @unsigned f@ = output flags
420 * Use: Emits a string to a stream.
423 extern void putstring(FILE */
*fp*/
, const char */
*p*/
, unsigned /*f*/);
425 /*----- Lists of things ---------------------------------------------------*/
427 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
429 * Produce list of things. Requires @i@ and @w@ variables in scope.
430 * END-TEST and NAME-EXPR are in terms of @i@.
433 #define LIST(what, fp, end, name) do { \
434 fputs(what ":\n ", fp); \
436 for (i = 0; end; i++) { \
440 if (strlen(name) + w > 76) { \
442 w = 2 + strlen(name); \
445 w += strlen(name) + 1; \
453 #define STDLISTS(LI) \
454 LI("Hash functions", hash, \
455 ghashtab[i], ghashtab[i]->name) \
456 LI("Encryption schemes", enc, \
457 gciphertab[i], gciphertab[i]->name) \
458 LI("Message authentication schemes", mac, \
459 gmactab[i], gmactab[i]->name) \
460 LI("Elliptic curves", ec, \
461 ectab[i].name, ectab[i].name) \
462 LI("Diffie-Hellman groups", dh, \
463 ptab[i].name, ptab[i].name)
465 #define LIDECL(text, tag, test, name) \
466 static void show_##tag(void);
468 #define LIDEF(text, tag, test, name) \
469 static void show_##tag(void) \
472 LIST(text, stdout, test, name); \
475 #define LIENT(text, tag, test, name) \
476 { #tag, show_##tag },
483 #define MAKELISTTAB(listtab, LISTS) \
485 static const struct listent listtab[] = { \
491 extern int displaylists(const struct listent */
*listtab*/
,
492 char *const /*argv*/[]);
494 /*----- Progress indicators -----------------------------------------------*/
496 typedef struct fprogress
{
503 /* --- @fprogress_init@ --- *
505 * Arguments: @fprogress *f@ = progress context to be initialized
506 * @const char *name@ = file name string to show
507 * @FILE *fp@ = file we're reading from
509 * Returns: Zero on success, nonzero if the file's state is now broken.
511 * Use: Initializes a progress context. Nothing is actually
515 extern int fprogress_init(fprogress */
*f*/
,
516 const char */
*name*/
, FILE */
*fp*/
);
518 /* --- @fprogress_update@ --- *
520 * Arguments: @fprogress *f@ = progress context
521 * @size_t n@ = how much progress has been made
525 * Use: Maybe updates the display to show that some progress has been
529 extern void fprogress_update(fprogress */
*f*/
, size_t /*n*/);
531 /* --- @fprogress_clear@ --- *
533 * Arguments: @fprogress *f@ = progress context
537 * Use: Clears the progress display from the screen.
540 extern void fprogress_clear(fprogress */
*f*/
);
542 /* --- @fprogress_done@ --- *
544 * Arguments: @fprogress *f@ = progress context
548 * Use: Clear up the progress context and removes any display.
551 extern void fprogress_done(fprogress */
*f*/
);
553 /*----- Subcommand dispatch -----------------------------------------------*/
557 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
562 extern void version(FILE */
*fp*/
);
563 extern void help_global(FILE */
*fp*/
);
565 /* --- @findcmd@ --- *
567 * Arguments: @const cmd *cmds@ = pointer to command table
568 * @const char *name@ = a command name
570 * Returns: Pointer to the command structure.
572 * Use: Looks up a command by name. If the command isn't found, an
573 * error is reported and the program is terminated.
576 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
578 /* --- @sc_help@ --- *
580 * Arguments: @const cmd *cmds@ = pointer to command table
581 * @FILE *fp@ = output file handle
582 * @char *const *argv@ = remaining arguments
586 * Use: Prints a help message, maybe with help about subcommands.
589 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
590 char *const */
*argv*/
);
592 /*----- That's all, folks -------------------------------------------------*/