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 ------------------------------------------------------*/
42 #include <mLib/dstr.h>
49 /*----- Data structures ---------------------------------------------------*/
51 /* --- Key encapsulation --- */
54 const struct kemops *ops;
58 const gccipher *c, *cx;
62 typedef struct kemops {
63 const key_fetchdef *kf; /* Key fetching structure */
64 size_t kdsz; /* Size of the key-data structure */
65 kem *(*init)(key */*k*/, void */*kd*/);
66 int (*doit)(kem */*k*/, dstr */*d*/, ghash */*h*/);
67 const char *(*check)(kem */*k*/);
68 void (*destroy)(kem */*k*/);
77 extern const struct kemtab kemtab[];
82 const struct sigops *ops;
88 typedef struct sigops {
89 const key_fetchdef *kf; /* Key fetching structure */
90 size_t kdsz; /* Size of the key-data structure */
91 sig *(*init)(key */*k*/, void */*kd*/, const gchash */*hc*/);
92 int (*doit)(sig */*s*/, dstr */*d*/);
93 const char *(*check)(sig */*s*/);
94 void (*destroy)(sig */*s*/);
99 const sigops *signops;
100 const sigops *verifyops;
104 extern const struct sigtab sigtab[];
106 /* --- Data encoding --- */
109 const struct encops *ops;
113 typedef struct encops {
115 const char *rmode, *wmode;
116 enc *(*initenc)(FILE */*fp*/, const char */*msg*/);
117 enc *(*initdec)(FILE */*fp*/,
118 int (*/*func*/)(const char *, void *), void */*p*/);
119 int (*read)(enc */*e*/, void */*p*/, size_t /*sz*/);
120 int (*write)(enc */*e*/, const void */*p*/, size_t /*sz*/);
121 int (*encdone)(enc */*e*/);
122 int (*decdone)(enc */*e*/);
123 void (*destroy)(enc */*e*/);
126 extern const encops enctab[];
128 /*----- Functions provided ------------------------------------------------*/
130 /* --- @getkem@ --- *
132 * Arguments: @key *k@ = the key to load
133 * @const char *app@ = application name
134 * @int wantpriv@ = nonzero if we want to decrypt
136 * Returns: A key-encapsulating thing.
141 extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/);
143 /* --- @setupkem@ --- *
145 * Arguments: @kem *k@ = key-encapsulation thing
146 * @dstr *d@ = key-encapsulation data
147 * @gcipher **cx@ = key-expansion function (for IVs)
148 * @gcipher **c@ = where to put initialized encryption scheme
149 * @gmac **m@ = where to put initialized MAC
151 * Returns: Zero for success, nonzero on faliure.
153 * Use: Initializes all the various symmetric things from a KEM.
156 extern int setupkem(kem */*k*/, dstr */*d*/,
157 gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/);
159 /* --- @freekem@ --- *
161 * Arguments: @kem *k@ = key-encapsulation thing
165 * Use: Frees up a key-encapsulation thing.
168 extern void freekem(kem */*k*/);
170 /* --- @getsig@ --- *
172 * Arguments: @key *k@ = the key to load
173 * @const char *app@ = application name
174 * @int wantpriv@ = nonzero if we want to sign
176 * Returns: A signature-making thing.
178 * Use: Loads a key and starts hashing.
181 extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/);
183 /* --- @freesig@ --- *
185 * Arguments: @sig *s@ = signature-making thing
189 * Use: Frees up a signature-making thing
192 extern void freesig(sig */*s*/);
194 /* --- @getenc@ --- *
196 * Arguments: @const char *enc@ = name of wanted encoding
198 * Returns: Pointer to encoder operations.
200 * Use: Finds a named encoder or decoder.
203 extern const encops *getenc(const char */*enc*/);
205 /* --- @checkbdry@ --- *
207 * Arguments: @const char *b@ = boundary string found
208 * @void *p@ = boundary string wanted
210 * Returns: Nonzero if the boundary string is the one we wanted.
212 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
215 extern int checkbdry(const char */*b*/, void */*p*/);
217 /* --- @initenc@ --- *
219 * Arguments: @const encops *eo@ = operations (from @getenc@)
220 * @FILE *fp@ = file handle to attach
221 * @const char *msg@ = banner message
223 * Returns: The encoder object.
225 * Use: Initializes an encoder.
228 extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
230 /* --- @initdec@ --- *
232 * Arguments: @const encops *eo@ = operations (from @getenc@)
233 * @FILE *fp@ = file handle to attach
234 * @int (*func)(const char *, void *)@ = banner check function
235 * @void *p@ = argument for @func@
237 * Returns: The encoder object.
239 * Use: Initializes an encoder.
242 extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
243 int (*/*func*/)(const char *, void *), void */*p*/);
245 /* --- @freeenc@ --- *
247 * Arguments: @enc *e@ = encoder object
251 * Use: Frees an encoder object.
254 extern void freeenc(enc */*e*/);
256 /* --- @cmd_encode@, @cmd_decode@ --- */
258 #define CMD_ENCODE { \
259 "encode", cmd_encode, \
260 "encode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
264 -f, --format=FORMAT Encode to FORMAT.\n\
265 -b, --boundary=LABEL PEM boundary is LABEL.\n\
266 -o, --output=FILE Write output to FILE.\n\
269 #define CMD_DECODE { \
270 "decode", cmd_decode, \
271 "decode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
275 -f, --format=FORMAT Decode from FORMAT.\n\
276 -b, --boundary=LABEL PEM boundary is LABEL.\n\
277 -o, --output=FILE Write output to FILE.\n\
280 extern int cmd_encode(int /*argc*/, char */*argv*/[]);
281 extern int cmd_decode(int /*argc*/, char */*argv*/[]);
283 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
285 * Produce list of things. Requires @i@ and @w@ variables in scope.
286 * END-TEST and NAME-EXPR are in terms of @i@.
289 #define LIST(what, fp, end, name) do { \
290 fputs(what ":\n ", fp); \
292 for (i = 0; end; i++) { \
296 if (strlen(name) + w > 76) { \
298 w = 2 + strlen(name); \
301 w += strlen(name) + 1; \
309 #define STDLISTS(LI) \
310 LI("Hash functions", hash, \
311 ghashtab[i], ghashtab[i]->name) \
312 LI("Encryption schemes", enc, \
313 gciphertab[i], gciphertab[i]->name) \
314 LI("Message authentication schemes", mac, \
315 gmactab[i], gmactab[i]->name) \
316 LI("Elliptic curves", ec, \
317 ectab[i].name, ectab[i].name) \
318 LI("Diffie-Hellman groups", dh, \
319 ptab[i].name, ptab[i].name)
321 #define LIDECL(text, tag, test, name) \
322 static void show_##tag(void);
324 #define LIDEF(text, tag, test, name) \
325 static void show_##tag(void) \
328 LIST(text, stdout, test, name); \
331 #define LIENT(text, tag, test, name) \
332 { #tag, show_##tag },
339 #define MAKELISTTAB(listtab, LISTS) \
341 static const struct listent listtab[] = { \
347 extern int displaylists(const struct listent */*listtab*/,
348 char *const /*argv*/[]);
350 /*----- Subcommand dispatch -----------------------------------------------*/
354 int (*cmd)(int /*argc*/, char */*argv*/[]);
359 extern void version(FILE */*fp*/);
360 extern void help_global(FILE */*fp*/);
362 /* --- @findcmd@ --- *
364 * Arguments: @const cmd *cmds@ = pointer to command table
365 * @const char *name@ = a command name
367 * Returns: Pointer to the command structure.
369 * Use: Looks up a command by name. If the command isn't found, an
370 * error is reported and the program is terminated.
373 const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
375 /* --- @sc_help@ --- *
377 * Arguments: @const cmd *cmds@ = pointer to command table
378 * @FILE *fp@ = output file handle
379 * @char *const *argv@ = remaining arguments
383 * Use: Prints a help message, maybe with help about subcommands.
386 extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
387 char *const */*argv*/);
389 /*----- That's all, folks -------------------------------------------------*/