6f92978323a0d6eb158fa2f0ff939a765ecb3089
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 /*----- Data structures ---------------------------------------------------*/
56 /* --- Progress indicators --- */
58 typedef struct fprogress
{
65 /* --- Key encapsulation --- */
68 const struct kemops
*ops
;
72 const gccipher
*c
, *cx
;
76 typedef struct kemops
{
77 const key_fetchdef
*kf
; /* Key fetching structure */
78 size_t kdsz
; /* Size of the key-data structure */
79 kem
*(*init
)(key */
*k*/
, void */
*kd*/
);
80 int (*doit
)(kem */
*k*/
, dstr */
*d*/
, ghash */
*h*/
);
81 const char *(*check
)(kem */
*k*/
);
82 void (*destroy
)(kem */
*k*/
);
91 extern const struct kemtab kemtab
[];
96 const struct sigops
*ops
;
103 typedef struct sigops
{
104 const key_fetchdef
*kf
; /* Key fetching structure */
105 size_t kdsz
; /* Size of the key-data structure */
106 sig
*(*init
)(key */
*k*/
, void */
*kd*/
, const gchash */
*hc*/
);
107 int (*doit
)(sig */
*s*/
, dstr */
*d*/
);
108 const char *(*check
)(sig */
*s*/
);
109 void (*destroy
)(sig */
*s*/
);
114 const sigops
*signops
;
115 const sigops
*verifyops
;
119 extern const struct sigtab sigtab
[];
121 /* --- Data encoding --- */
124 const struct encops
*ops
;
128 typedef struct encops
{
130 const char *rmode
, *wmode
;
132 enc
*(*initenc
)(FILE */
*fp*/
, const char */
*msg*/
);
133 enc
*(*initdec
)(FILE */
*fp*/
,
134 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
135 int (*read
)(enc */
*e*/
, void */
*p*/
, size_t /*sz*/);
136 int (*write
)(enc */
*e*/
, const void */
*p*/
, size_t /*sz*/);
137 int (*encdone
)(enc */
*e*/
);
138 int (*decdone
)(enc */
*e*/
);
139 void (*destroy
)(enc */
*e*/
);
142 extern const encops enctab
[];
144 /*----- Functions provided ------------------------------------------------*/
146 /* --- @getkem@ --- *
148 * Arguments: @key *k@ = the key to load
149 * @const char *app@ = application name
150 * @int wantpriv@ = nonzero if we want to decrypt
152 * Returns: A key-encapsulating thing.
157 extern kem
*getkem(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
159 /* --- @setupkem@ --- *
161 * Arguments: @kem *k@ = key-encapsulation thing
162 * @dstr *d@ = key-encapsulation data
163 * @gcipher **cx@ = key-expansion function (for IVs)
164 * @gcipher **c@ = where to put initialized encryption scheme
165 * @gmac **m@ = where to put initialized MAC
167 * Returns: Zero for success, nonzero on faliure.
169 * Use: Initializes all the various symmetric things from a KEM.
172 extern int setupkem(kem */
*k*/
, dstr */
*d*/
,
173 gcipher
**/
*cx*/
, gcipher
**/
*c*/
, gmac
**/
*m*/
);
175 /* --- @freekem@ --- *
177 * Arguments: @kem *k@ = key-encapsulation thing
181 * Use: Frees up a key-encapsulation thing.
184 extern void freekem(kem */
*k*/
);
186 /* --- @getsig@ --- *
188 * Arguments: @key *k@ = the key to load
189 * @const char *app@ = application name
190 * @int wantpriv@ = nonzero if we want to sign
192 * Returns: A signature-making thing.
194 * Use: Loads a key and starts hashing.
197 extern sig
*getsig(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
199 /* --- @freesig@ --- *
201 * Arguments: @sig *s@ = signature-making thing
205 * Use: Frees up a signature-making thing
208 extern void freesig(sig */
*s*/
);
210 /* --- @getenc@ --- *
212 * Arguments: @const char *enc@ = name of wanted encoding
214 * Returns: Pointer to encoder operations.
216 * Use: Finds a named encoder or decoder.
219 extern const encops
*getenc(const char */
*enc*/
);
221 /* --- @checkbdry@ --- *
223 * Arguments: @const char *b@ = boundary string found
224 * @void *p@ = boundary string wanted
226 * Returns: Nonzero if the boundary string is the one we wanted.
228 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
231 extern int checkbdry(const char */
*b*/
, void */
*p*/
);
233 /* --- @initenc@ --- *
235 * Arguments: @const encops *eo@ = operations (from @getenc@)
236 * @FILE *fp@ = file handle to attach
237 * @const char *msg@ = banner message
239 * Returns: The encoder object.
241 * Use: Initializes an encoder.
244 extern enc
*initenc(const encops */
*eo*/
, FILE */
*fp*/
, const char */
*msg*/
);
246 /* --- @initdec@ --- *
248 * Arguments: @const encops *eo@ = operations (from @getenc@)
249 * @FILE *fp@ = file handle to attach
250 * @int (*func)(const char *, void *)@ = banner check function
251 * @void *p@ = argument for @func@
253 * Returns: The encoder object.
255 * Use: Initializes an encoder.
258 extern enc
*initdec(const encops */
*eo*/
, FILE */
*fp*/
,
259 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
261 /* --- @freeenc@ --- *
263 * Arguments: @enc *e@ = encoder object
267 * Use: Frees an encoder object.
270 extern void freeenc(enc */
*e*/
);
272 /* --- @cmd_encode@, @cmd_decode@ --- */
274 #define CMD_ENCODE { \
275 "encode", cmd_encode, \
276 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
280 -f, --format=FORMAT Encode to FORMAT.\n\
281 -b, --boundary=LABEL PEM boundary is LABEL.\n\
282 -o, --output=FILE Write output to FILE.\n\
283 -p, --progress Show progress on large files.\n\
286 #define CMD_DECODE { \
287 "decode", cmd_decode, \
288 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
292 -f, --format=FORMAT Decode from FORMAT.\n\
293 -b, --boundary=LABEL PEM boundary is LABEL.\n\
294 -o, --output=FILE Write output to FILE.\n\
295 -p, --progress Show progress on large files.\n\
298 extern int cmd_encode(int /*argc*/, char */
*argv*/
[]);
299 extern int cmd_decode(int /*argc*/, char */
*argv*/
[]);
301 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
303 * Produce list of things. Requires @i@ and @w@ variables in scope.
304 * END-TEST and NAME-EXPR are in terms of @i@.
307 #define LIST(what, fp, end, name) do { \
308 fputs(what ":\n ", fp); \
310 for (i = 0; end; i++) { \
314 if (strlen(name) + w > 76) { \
316 w = 2 + strlen(name); \
319 w += strlen(name) + 1; \
327 #define STDLISTS(LI) \
328 LI("Hash functions", hash, \
329 ghashtab[i], ghashtab[i]->name) \
330 LI("Encryption schemes", enc, \
331 gciphertab[i], gciphertab[i]->name) \
332 LI("Message authentication schemes", mac, \
333 gmactab[i], gmactab[i]->name) \
334 LI("Elliptic curves", ec, \
335 ectab[i].name, ectab[i].name) \
336 LI("Diffie-Hellman groups", dh, \
337 ptab[i].name, ptab[i].name)
339 #define LIDECL(text, tag, test, name) \
340 static void show_##tag(void);
342 #define LIDEF(text, tag, test, name) \
343 static void show_##tag(void) \
346 LIST(text, stdout, test, name); \
349 #define LIENT(text, tag, test, name) \
350 { #tag, show_##tag },
357 #define MAKELISTTAB(listtab, LISTS) \
359 static const struct listent listtab[] = { \
365 extern int displaylists(const struct listent */
*listtab*/
,
366 char *const /*argv*/[]);
368 /*----- Subcommand dispatch -----------------------------------------------*/
372 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
377 extern void version(FILE */
*fp*/
);
378 extern void help_global(FILE */
*fp*/
);
380 /* --- @findcmd@ --- *
382 * Arguments: @const cmd *cmds@ = pointer to command table
383 * @const char *name@ = a command name
385 * Returns: Pointer to the command structure.
387 * Use: Looks up a command by name. If the command isn't found, an
388 * error is reported and the program is terminated.
391 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
393 /* --- @sc_help@ --- *
395 * Arguments: @const cmd *cmds@ = pointer to command table
396 * @FILE *fp@ = output file handle
397 * @char *const *argv@ = remaining arguments
401 * Use: Prints a help message, maybe with help about subcommands.
404 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
405 char *const */
*argv*/
);
407 /*----- Progress indicators -----------------------------------------------*/
409 /* --- @fprogress_init@ --- *
411 * Arguments: @fprogress *f@ = progress context to be initialized
412 * @const char *name@ = file name string to show
413 * @FILE *fp@ = file we're reading from
415 * Returns: Zero on success, nonzero if the file's state is now broken.
417 * Use: Initializes a progress context. Nothing is actually
421 extern int fprogress_init(fprogress */
*f*/
,
422 const char */
*name*/
, FILE */
*fp*/
);
424 /* --- @fprogress_update@ --- *
426 * Arguments: @fprogress *f@ = progress context
427 * @size_t n@ = how much progress has been made
431 * Use: Maybe updates the display to show that some progress has been
435 extern void fprogress_update(fprogress */
*f*/
, size_t /*n*/);
437 /* --- @fprogress_clear@ --- *
439 * Arguments: @fprogress *f@ = progress context
443 * Use: Clears the progress display from the screen.
446 extern void fprogress_clear(fprogress */
*f*/
);
448 /* --- @fprogress_done@ --- *
450 * Arguments: @fprogress *f@ = progress context
454 * Use: Clear up the progress context and removes any display.
457 extern void fprogress_done(fprogress */
*f*/
);
459 /*----- That's all, folks -------------------------------------------------*/