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
;
89 typedef struct sigops
{
90 const key_fetchdef
*kf
; /* Key fetching structure */
91 size_t kdsz
; /* Size of the key-data structure */
92 sig
*(*init
)(key */
*k*/
, void */
*kd*/
, const gchash */
*hc*/
);
93 int (*doit
)(sig */
*s*/
, dstr */
*d*/
);
94 const char *(*check
)(sig */
*s*/
);
95 void (*destroy
)(sig */
*s*/
);
100 const sigops
*signops
;
101 const sigops
*verifyops
;
105 extern const struct sigtab sigtab
[];
107 /* --- Data encoding --- */
110 const struct encops
*ops
;
114 typedef struct encops
{
116 const char *rmode
, *wmode
;
117 enc
*(*initenc
)(FILE */
*fp*/
, const char */
*msg*/
);
118 enc
*(*initdec
)(FILE */
*fp*/
,
119 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
120 int (*read
)(enc */
*e*/
, void */
*p*/
, size_t /*sz*/);
121 int (*write
)(enc */
*e*/
, const void */
*p*/
, size_t /*sz*/);
122 int (*encdone
)(enc */
*e*/
);
123 int (*decdone
)(enc */
*e*/
);
124 void (*destroy
)(enc */
*e*/
);
127 extern const encops enctab
[];
129 /*----- Functions provided ------------------------------------------------*/
131 /* --- @getkem@ --- *
133 * Arguments: @key *k@ = the key to load
134 * @const char *app@ = application name
135 * @int wantpriv@ = nonzero if we want to decrypt
137 * Returns: A key-encapsulating thing.
142 extern kem
*getkem(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
144 /* --- @setupkem@ --- *
146 * Arguments: @kem *k@ = key-encapsulation thing
147 * @dstr *d@ = key-encapsulation data
148 * @gcipher **cx@ = key-expansion function (for IVs)
149 * @gcipher **c@ = where to put initialized encryption scheme
150 * @gmac **m@ = where to put initialized MAC
152 * Returns: Zero for success, nonzero on faliure.
154 * Use: Initializes all the various symmetric things from a KEM.
157 extern int setupkem(kem */
*k*/
, dstr */
*d*/
,
158 gcipher
**/
*cx*/
, gcipher
**/
*c*/
, gmac
**/
*m*/
);
160 /* --- @freekem@ --- *
162 * Arguments: @kem *k@ = key-encapsulation thing
166 * Use: Frees up a key-encapsulation thing.
169 extern void freekem(kem */
*k*/
);
171 /* --- @getsig@ --- *
173 * Arguments: @key *k@ = the key to load
174 * @const char *app@ = application name
175 * @int wantpriv@ = nonzero if we want to sign
177 * Returns: A signature-making thing.
179 * Use: Loads a key and starts hashing.
182 extern sig
*getsig(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
184 /* --- @freesig@ --- *
186 * Arguments: @sig *s@ = signature-making thing
190 * Use: Frees up a signature-making thing
193 extern void freesig(sig */
*s*/
);
195 /* --- @getenc@ --- *
197 * Arguments: @const char *enc@ = name of wanted encoding
199 * Returns: Pointer to encoder operations.
201 * Use: Finds a named encoder or decoder.
204 extern const encops
*getenc(const char */
*enc*/
);
206 /* --- @checkbdry@ --- *
208 * Arguments: @const char *b@ = boundary string found
209 * @void *p@ = boundary string wanted
211 * Returns: Nonzero if the boundary string is the one we wanted.
213 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
216 extern int checkbdry(const char */
*b*/
, void */
*p*/
);
218 /* --- @initenc@ --- *
220 * Arguments: @const encops *eo@ = operations (from @getenc@)
221 * @FILE *fp@ = file handle to attach
222 * @const char *msg@ = banner message
224 * Returns: The encoder object.
226 * Use: Initializes an encoder.
229 extern enc
*initenc(const encops */
*eo*/
, FILE */
*fp*/
, const char */
*msg*/
);
231 /* --- @initdec@ --- *
233 * Arguments: @const encops *eo@ = operations (from @getenc@)
234 * @FILE *fp@ = file handle to attach
235 * @int (*func)(const char *, void *)@ = banner check function
236 * @void *p@ = argument for @func@
238 * Returns: The encoder object.
240 * Use: Initializes an encoder.
243 extern enc
*initdec(const encops */
*eo*/
, FILE */
*fp*/
,
244 int (*/
*func*/
)(const char *, void *), void */
*p*/
);
246 /* --- @freeenc@ --- *
248 * Arguments: @enc *e@ = encoder object
252 * Use: Frees an encoder object.
255 extern void freeenc(enc */
*e*/
);
257 /* --- @cmd_encode@, @cmd_decode@ --- */
259 #define CMD_ENCODE { \
260 "encode", cmd_encode, \
261 "encode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
265 -f, --format=FORMAT Encode to FORMAT.\n\
266 -b, --boundary=LABEL PEM boundary is LABEL.\n\
267 -o, --output=FILE Write output to FILE.\n\
270 #define CMD_DECODE { \
271 "decode", cmd_decode, \
272 "decode [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
276 -f, --format=FORMAT Decode from FORMAT.\n\
277 -b, --boundary=LABEL PEM boundary is LABEL.\n\
278 -o, --output=FILE Write output to FILE.\n\
281 extern int cmd_encode(int /*argc*/, char */
*argv*/
[]);
282 extern int cmd_decode(int /*argc*/, char */
*argv*/
[]);
284 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
286 * Produce list of things. Requires @i@ and @w@ variables in scope.
287 * END-TEST and NAME-EXPR are in terms of @i@.
290 #define LIST(what, fp, end, name) do { \
291 fputs(what ":\n ", fp); \
293 for (i = 0; end; i++) { \
297 if (strlen(name) + w > 76) { \
299 w = 2 + strlen(name); \
302 w += strlen(name) + 1; \
310 #define STDLISTS(LI) \
311 LI("Hash functions", hash, \
312 ghashtab[i], ghashtab[i]->name) \
313 LI("Encryption schemes", enc, \
314 gciphertab[i], gciphertab[i]->name) \
315 LI("Message authentication schemes", mac, \
316 gmactab[i], gmactab[i]->name) \
317 LI("Elliptic curves", ec, \
318 ectab[i].name, ectab[i].name) \
319 LI("Diffie-Hellman groups", dh, \
320 ptab[i].name, ptab[i].name)
322 #define LIDECL(text, tag, test, name) \
323 static void show_##tag(void);
325 #define LIDEF(text, tag, test, name) \
326 static void show_##tag(void) \
329 LIST(text, stdout, test, name); \
332 #define LIENT(text, tag, test, name) \
333 { #tag, show_##tag },
340 #define MAKELISTTAB(listtab, LISTS) \
342 static const struct listent listtab[] = { \
348 extern int displaylists(const struct listent */
*listtab*/
,
349 char *const /*argv*/[]);
351 /*----- Subcommand dispatch -----------------------------------------------*/
355 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
360 extern void version(FILE */
*fp*/
);
361 extern void help_global(FILE */
*fp*/
);
363 /* --- @findcmd@ --- *
365 * Arguments: @const cmd *cmds@ = pointer to command table
366 * @const char *name@ = a command name
368 * Returns: Pointer to the command structure.
370 * Use: Looks up a command by name. If the command isn't found, an
371 * error is reported and the program is terminated.
374 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
376 /* --- @sc_help@ --- *
378 * Arguments: @const cmd *cmds@ = pointer to command table
379 * @FILE *fp@ = output file handle
380 * @char *const *argv@ = remaining arguments
384 * Use: Prints a help message, maybe with help about subcommands.
387 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
388 char *const */
*argv*/
);
390 /*----- That's all, folks -------------------------------------------------*/