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 /*----- Lists of things ---------------------------------------------------*/
294 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
296 * Produce list of things. Requires @i@ and @w@ variables in scope.
297 * END-TEST and NAME-EXPR are in terms of @i@.
300 #define LIST(what, fp, end, name) do { \
301 fputs(what ":\n ", fp); \
303 for (i = 0; end; i++) { \
307 if (strlen(name) + w > 76) { \
309 w = 2 + strlen(name); \
312 w += strlen(name) + 1; \
320 #define STDLISTS(LI) \
321 LI("Hash functions", hash, \
322 ghashtab[i], ghashtab[i]->name) \
323 LI("Encryption schemes", enc, \
324 gciphertab[i], gciphertab[i]->name) \
325 LI("Message authentication schemes", mac, \
326 gmactab[i], gmactab[i]->name) \
327 LI("Elliptic curves", ec, \
328 ectab[i].name, ectab[i].name) \
329 LI("Diffie-Hellman groups", dh, \
330 ptab[i].name, ptab[i].name)
332 #define LIDECL(text, tag, test, name) \
333 static void show_##tag(void);
335 #define LIDEF(text, tag, test, name) \
336 static void show_##tag(void) \
339 LIST(text, stdout, test, name); \
342 #define LIENT(text, tag, test, name) \
343 { #tag, show_##tag },
350 #define MAKELISTTAB(listtab, LISTS) \
352 static const struct listent listtab[] = { \
358 extern int displaylists(const struct listent */
*listtab*/
,
359 char *const /*argv*/[]);
361 /*----- Progress indicators -----------------------------------------------*/
363 typedef struct fprogress
{
370 /* --- @fprogress_init@ --- *
372 * Arguments: @fprogress *f@ = progress context to be initialized
373 * @const char *name@ = file name string to show
374 * @FILE *fp@ = file we're reading from
376 * Returns: Zero on success, nonzero if the file's state is now broken.
378 * Use: Initializes a progress context. Nothing is actually
382 extern int fprogress_init(fprogress */
*f*/
,
383 const char */
*name*/
, FILE */
*fp*/
);
385 /* --- @fprogress_update@ --- *
387 * Arguments: @fprogress *f@ = progress context
388 * @size_t n@ = how much progress has been made
392 * Use: Maybe updates the display to show that some progress has been
396 extern void fprogress_update(fprogress */
*f*/
, size_t /*n*/);
398 /* --- @fprogress_clear@ --- *
400 * Arguments: @fprogress *f@ = progress context
404 * Use: Clears the progress display from the screen.
407 extern void fprogress_clear(fprogress */
*f*/
);
409 /* --- @fprogress_done@ --- *
411 * Arguments: @fprogress *f@ = progress context
415 * Use: Clear up the progress context and removes any display.
418 extern void fprogress_done(fprogress */
*f*/
);
420 /*----- Subcommand dispatch -----------------------------------------------*/
424 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
429 extern void version(FILE */
*fp*/
);
430 extern void help_global(FILE */
*fp*/
);
432 /* --- @findcmd@ --- *
434 * Arguments: @const cmd *cmds@ = pointer to command table
435 * @const char *name@ = a command name
437 * Returns: Pointer to the command structure.
439 * Use: Looks up a command by name. If the command isn't found, an
440 * error is reported and the program is terminated.
443 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
445 /* --- @sc_help@ --- *
447 * Arguments: @const cmd *cmds@ = pointer to command table
448 * @FILE *fp@ = output file handle
449 * @char *const *argv@ = remaining arguments
453 * Use: Prints a help message, maybe with help about subcommands.
456 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
457 char *const */
*argv*/
);
459 /*----- That's all, folks -------------------------------------------------*/