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*/
, const char */
*msg*/
);
118 int (*read
)(enc */
*e*/
, void */
*p*/
, size_t /*sz*/);
119 int (*write
)(enc */
*e*/
, const void */
*p*/
, size_t /*sz*/);
120 int (*encdone
)(enc */
*e*/
);
121 int (*decdone
)(enc */
*e*/
);
122 void (*destroy
)(enc */
*e*/
);
125 extern const encops enctab
[];
127 /*----- Functions provided ------------------------------------------------*/
129 /* --- @getkem@ --- *
131 * Arguments: @key *k@ = the key to load
132 * @const char *app@ = application name
133 * @int wantpriv@ = nonzero if we want to decrypt
135 * Returns: A key-encapsulating thing.
140 extern kem
*getkem(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
142 /* --- @setupkem@ --- *
144 * Arguments: @kem *k@ = key-encapsulation thing
145 * @dstr *d@ = key-encapsulation data
146 * @gcipher **cx@ = key-expansion function (for IVs)
147 * @gcipher **c@ = where to put initialized encryption scheme
148 * @gmac **m@ = where to put initialized MAC
150 * Returns: Zero for success, nonzero on faliure.
152 * Use: Initializes all the various symmetric things from a KEM.
155 extern int setupkem(kem */
*k*/
, dstr */
*d*/
,
156 gcipher
**/
*cx*/
, gcipher
**/
*c*/
, gmac
**/
*m*/
);
158 /* --- @freekem@ --- *
160 * Arguments: @kem *k@ = key-encapsulation thing
164 * Use: Frees up a key-encapsulation thing.
167 extern void freekem(kem */
*k*/
);
169 /* --- @getsig@ --- *
171 * Arguments: @key *k@ = the key to load
172 * @const char *app@ = application name
173 * @int wantpriv@ = nonzero if we want to sign
175 * Returns: A signature-making thing.
177 * Use: Loads a key and starts hashing.
180 extern sig
*getsig(key */
*k*/
, const char */
*app*/
, int /*wantpriv*/);
182 /* --- @freesig@ --- *
184 * Arguments: @sig *s@ = signature-making thing
188 * Use: Frees up a signature-making thing
191 extern void freesig(sig */
*s*/
);
193 /* --- @getenc@ --- *
195 * Arguments: @const char *enc@ = name of wanted encoding
197 * Returns: Pointer to encoder operations.
199 * Use: Finds a named encoder or decoder.
202 extern const encops
*getenc(const char */
*enc*/
);
204 /* --- @initenc@ --- *
206 * Arguments: @const encops *eo@ = operations (from @getenc@)
207 * @FILE *fp@ = file handle to attach
208 * @const char *msg@ = banner message
209 * @int wantenc@ = nonzero if we want to encode
211 * Returns: The encoder object.
213 * Use: Initializes an encoder.
216 extern enc
*initenc(const encops */
*eo*/
, FILE */
*fp*/
,
217 const char */
*msg*/
, int /*wantenc*/);
219 /* --- @freeenc@ --- *
221 * Arguments: @enc *e@ = encoder object
225 * Use: Frees an encoder object.
228 extern void freeenc(enc */
*e*/
);
230 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
232 * Produce list of things. Requires @i@ and @w@ variables in scope.
233 * END-TEST and NAME-EXPR are in terms of @i@.
236 #define LIST(what, fp, end, name) do { \
237 fputs(what ":\n ", fp); \
239 for (i = 0; end; i++) { \
243 if (strlen(name) + w > 76) { \
245 w = 2 + strlen(name); \
248 w += strlen(name) + 1; \
256 #define STDLISTS(LI) \
257 LI("Hash functions", hash, \
258 ghashtab[i], ghashtab[i]->name) \
259 LI("Encryption schemes", enc, \
260 gciphertab[i], gciphertab[i]->name) \
261 LI("Message authentication schemes", mac, \
262 gmactab[i], gmactab[i]->name) \
263 LI("Elliptic curves", ec, \
264 ectab[i].name, ectab[i].name) \
265 LI("Diffie-Hellman groups", dh, \
266 ptab[i].name, ptab[i].name)
268 #define LIDECL(text, tag, test, name) \
269 static void show_##tag(void);
271 #define LIDEF(text, tag, test, name) \
272 static void show_##tag(void) \
275 LIST(text, stdout, test, name); \
278 #define LIENT(text, tag, test, name) \
279 { #tag, show_##tag },
286 #define MAKELISTTAB(listtab, LISTS) \
288 static const struct listent listtab[] = { \
294 extern int displaylists(const struct listent */
*listtab*/
,
295 char *const /*argv*/[]);
297 /*----- Subcommand dispatch -----------------------------------------------*/
301 int (*cmd
)(int /*argc*/, char */
*argv*/
[]);
306 extern void version(FILE */
*fp*/
);
307 extern void help_global(FILE */
*fp*/
);
309 /* --- @findcmd@ --- *
311 * Arguments: @const cmd *cmds@ = pointer to command table
312 * @const char *name@ = a command name
314 * Returns: Pointer to the command structure.
316 * Use: Looks up a command by name. If the command isn't found, an
317 * error is reported and the program is terminated.
320 const cmd
*findcmd(const cmd */
*cmds*/
, const char */
*name*/
);
322 /* --- @sc_help@ --- *
324 * Arguments: @const cmd *cmds@ = pointer to command table
325 * @FILE *fp@ = output file handle
326 * @char *const *argv@ = remaining arguments
330 * Use: Prints a help message, maybe with help about subcommands.
333 extern void sc_help(const cmd */
*cmds*/
, FILE */
*fp*/
,
334 char *const */
*argv*/
);
336 /*----- That's all, folks -------------------------------------------------*/