mpint.c (touint): Compare unsigned with unsigned, not unsigned long.
[u/mdw/catacomb] / cc.h
1 /* -*-c-*-
2 *
3 * $Id$
4 *
5 * Catcrypt common stuff
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 #ifndef CATACOMB_CC_H
31 #define CATACOMB_CC_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #if _FILE_OFFSET_BITS != 64
40 # error "Must set _FILE_OFFSET_BITS to 64."
41 #endif
42
43 #include <stdio.h>
44 #include <string.h>
45 #include <time.h>
46
47 #include <mLib/dstr.h>
48
49 #include "key.h"
50 #include "gcipher.h"
51 #include "ghash.h"
52 #include "gmac.h"
53
54 /*----- Cryptographic object tables ---------------------------------------*/
55
56 /* --- Key encapsulation --- */
57
58 typedef struct kem {
59 const struct kemops *ops;
60 key_packdef *kp;
61 void *kd;
62 const gchash *h;
63 const gccipher *c, *cx;
64 const gcmac *m;
65 } kem;
66
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*/);
74 } kemops;
75
76 struct kemtab {
77 const char *name;
78 const kemops *encops;
79 const kemops *decops;
80 };
81
82 extern const struct kemtab kemtab[];
83
84 /* --- @getkem@ --- *
85 *
86 * Arguments: @key *k@ = the key to load
87 * @const char *app@ = application name
88 * @int wantpriv@ = nonzero if we want to decrypt
89 *
90 * Returns: A key-encapsulating thing.
91 *
92 * Use: Loads a key.
93 */
94
95 extern kem *getkem(key */*k*/, const char */*app*/, int /*wantpriv*/);
96
97 /* --- @setupkem@ --- *
98 *
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
104 *
105 * Returns: Zero for success, nonzero on faliure.
106 *
107 * Use: Initializes all the various symmetric things from a KEM.
108 */
109
110 extern int setupkem(kem */*k*/, dstr */*d*/,
111 gcipher **/*cx*/, gcipher **/*c*/, gmac **/*m*/);
112
113 /* --- @freekem@ --- *
114 *
115 * Arguments: @kem *k@ = key-encapsulation thing
116 *
117 * Returns: ---
118 *
119 * Use: Frees up a key-encapsulation thing.
120 */
121
122 extern void freekem(kem */*k*/);
123
124 /* --- Signing --- */
125
126 typedef struct sig {
127 const struct sigops *ops;
128 key_packdef *kp;
129 void *kd;
130 const gchash *ch;
131 ghash *h;
132 } sig;
133
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*/);
141 } sigops;
142
143 struct sigtab {
144 const char *name;
145 const sigops *signops;
146 const sigops *verifyops;
147 const gchash *ch;
148 };
149
150 extern const struct sigtab sigtab[];
151
152 /* --- @getsig@ --- *
153 *
154 * Arguments: @key *k@ = the key to load
155 * @const char *app@ = application name
156 * @int wantpriv@ = nonzero if we want to sign
157 *
158 * Returns: A signature-making thing.
159 *
160 * Use: Loads a key and starts hashing.
161 */
162
163 extern sig *getsig(key */*k*/, const char */*app*/, int /*wantpriv*/);
164
165 /* --- @freesig@ --- *
166 *
167 * Arguments: @sig *s@ = signature-making thing
168 *
169 * Returns: ---
170 *
171 * Use: Frees up a signature-making thing
172 */
173
174 extern void freesig(sig */*s*/);
175
176 /*----- File encodings ----------------------------------------------------*/
177
178 /* --- Data encoding --- */
179
180 typedef struct enc {
181 const struct encops *ops;
182 FILE *fp;
183 } enc;
184
185 typedef struct encops {
186 const char *name;
187 const char *rmode, *wmode;
188 int nraw, ncook;
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*/);
197 } encops;
198
199 extern const encops enctab[];
200
201 /* --- @getenc@ --- *
202 *
203 * Arguments: @const char *enc@ = name of wanted encoding
204 *
205 * Returns: Pointer to encoder operations.
206 *
207 * Use: Finds a named encoder or decoder.
208 */
209
210 extern const encops *getenc(const char */*enc*/);
211
212 /* --- @checkbdry@ --- *
213 *
214 * Arguments: @const char *b@ = boundary string found
215 * @void *p@ = boundary string wanted
216 *
217 * Returns: Nonzero if the boundary string is the one we wanted.
218 *
219 * Use: Pass as @func@ to @initdec@ if you just want a simple life.
220 */
221
222 extern int checkbdry(const char */*b*/, void */*p*/);
223
224 /* --- @initenc@ --- *
225 *
226 * Arguments: @const encops *eo@ = operations (from @getenc@)
227 * @FILE *fp@ = file handle to attach
228 * @const char *msg@ = banner message
229 *
230 * Returns: The encoder object.
231 *
232 * Use: Initializes an encoder.
233 */
234
235 extern enc *initenc(const encops */*eo*/, FILE */*fp*/, const char */*msg*/);
236
237 /* --- @initdec@ --- *
238 *
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@
243 *
244 * Returns: The encoder object.
245 *
246 * Use: Initializes an encoder.
247 */
248
249 extern enc *initdec(const encops */*eo*/, FILE */*fp*/,
250 int (*/*func*/)(const char *, void *), void */*p*/);
251
252 /* --- @freeenc@ --- *
253 *
254 * Arguments: @enc *e@ = encoder object
255 *
256 * Returns: ---
257 *
258 * Use: Frees an encoder object.
259 */
260
261 extern void freeenc(enc */*e*/);
262
263 /* --- @cmd_encode@, @cmd_decode@ --- */
264
265 #define CMD_ENCODE { \
266 "encode", cmd_encode, \
267 "encode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
268 "\
269 Options:\n\
270 \n\
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\
275 " }
276
277 #define CMD_DECODE { \
278 "decode", cmd_decode, \
279 "decode [-p] [-f FORMAT] [-b LABEL] [-o OUTPUT] [FILE]", \
280 "\
281 Options:\n\
282 \n\
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\
287 " }
288
289 extern int cmd_encode(int /*argc*/, char */*argv*/[]);
290 extern int cmd_decode(int /*argc*/, char */*argv*/[]);
291
292 /*----- Hash encoding functions -------------------------------------------*/
293
294 /* --- Table --- */
295
296 #define ENCODINGS(_) \
297 _(HEX, hex) \
298 _(BASE64, base64) \
299 _(BASE32, base32)
300
301 enum {
302 #define ENUM(tag, name) ENC_##tag,
303 ENCODINGS(ENUM)
304 #undef ENUM
305 ENC_LIMIT
306 };
307
308 typedef struct encodeops {
309 const char *name;
310 void (*put)(const octet *, size_t, FILE *);
311 size_t (*get)(const char *, octet *, size_t, char **);
312 } encodeops;
313
314 extern const encodeops encodingtab[];
315
316 /* --- @getencoding@ --- *
317 *
318 * Arguments: @const char *ename@ = encoding name
319 *
320 * Returns: Pointer to encoding table entry, or null.
321 *
322 * Use: Finds an encoding entry given its name.
323 */
324
325 extern const encodeops *getencoding(const char */*ename*/);
326
327 /*----- File hashing ------------------------------------------------------*/
328
329 /* --- @gethash@ --- *
330 *
331 * Arguments: @const char *name@ = pointer to name string
332 *
333 * Returns: Pointer to appropriate hash class.
334 *
335 * Use: Chooses a hash function by name.
336 */
337
338 extern const gchash *gethash(const char */*name*/);
339
340 /* --- @fhash@ --- *
341 *
342 * Arguments: @const gchash *gch@ = pointer to hash function to use
343 * @unsigned f@ = flags to set
344 * @const char *file@ = file name to be hashed (null for stdin)
345 * @void *buf@ = pointer to hash output buffer
346 *
347 * Returns: Zero if it worked, nonzero on error.
348 *
349 * Use: Hashes a file.
350 */
351
352 #define FHF_BINARY 256u
353 #define FHF_PROGRESS 512u
354
355 #define FHF_MASK 384u
356
357 extern int fhash(const gchash */*gch*/, unsigned /*f*/,
358 const char */*file*/, void */*buf*/);
359
360 /* --- @hfparse@ --- *
361 *
362 * Arguments: @hfpctx *hfp@ = pointer to the context structure
363 *
364 * Returns: A code indicating what happened.
365 *
366 * Use: Parses a line from the input file.
367 */
368
369 enum { /* Meaning and members set */
370 HF_FILE, /* File hash: @dline@ and @hbuf@ */
371 HF_ENC, /* Encoding: @ee@ */
372 HF_HASH, /* Hash function: @gch@ */
373 HF_ESC, /* Name escape: @f@ */
374 HF_EOF, /* End of file */
375 HF_BAD /* Unrecognized line */
376 };
377
378 typedef struct hfpctx {
379 unsigned f; /* Flags to read */
380 #define HFF_ESCAPE 1u /* File names are escaped */
381 FILE *fp; /* Input file to read */
382 dstr *dline; /* Line contents, corrupted */
383 const gchash *gch; /* Hash function to use */
384 const encodeops *ee; /* Encoding to apply to hashes */
385 dstr *dfile; /* File name for @HF_FILE@ lines */
386 octet *hbuf; /* Output buffer for hash data */
387 } hfpctx;
388
389 extern int hfparse(hfpctx */*hfp*/);
390
391 /*----- String I/O --------------------------------------------------------*/
392
393 #define GSF_RAW 4096u
394 #define GSF_FILE 0u
395 #define GSF_STRING 8192u
396
397 #define GSF_MASK 61440u
398
399 /* --- @getstring@ --- *
400 *
401 * Arguments: @void *in@ = input source
402 * @dstr *d@ = destination string
403 * @unsigned f@ = input flags
404 *
405 * Returns: Zero if OK, nonzero on end-of-file.
406 *
407 * Use: Reads a filename (or something similar) from a stream.
408 */
409
410 extern int getstring(void */*in*/, dstr */*d*/, unsigned /*f*/);
411
412 /* --- @putstring@ --- *
413 *
414 * Arguments: @FILE *fp@ = stream to write on
415 * @const char *p@ = pointer to text
416 * @unsigned f@ = output flags
417 *
418 * Returns: ---
419 *
420 * Use: Emits a string to a stream.
421 */
422
423 extern void putstring(FILE */*fp*/, const char */*p*/, unsigned /*f*/);
424
425 /*----- Lists of things ---------------------------------------------------*/
426
427 /* --- @LIST(STRING, FP, END-TEST, NAME-EXPR)@ --- *
428 *
429 * Produce list of things. Requires @i@ and @w@ variables in scope.
430 * END-TEST and NAME-EXPR are in terms of @i@.
431 */
432
433 #define LIST(what, fp, end, name) do { \
434 fputs(what ":\n ", fp); \
435 w = 2; \
436 for (i = 0; end; i++) { \
437 if (w == 2) \
438 w += strlen(name); \
439 else { \
440 if (strlen(name) + w > 76) { \
441 fputs("\n ", fp); \
442 w = 2 + strlen(name); \
443 } else { \
444 fputc(' ', fp); \
445 w += strlen(name) + 1; \
446 } \
447 } \
448 fputs(name, fp); \
449 } \
450 fputc('\n', fp); \
451 } while (0)
452
453 #define STDLISTS(LI) \
454 LI("Hash functions", hash, \
455 ghashtab[i], ghashtab[i]->name) \
456 LI("Encryption schemes", enc, \
457 gciphertab[i], gciphertab[i]->name) \
458 LI("Message authentication schemes", mac, \
459 gmactab[i], gmactab[i]->name) \
460 LI("Elliptic curves", ec, \
461 ectab[i].name, ectab[i].name) \
462 LI("Diffie-Hellman groups", dh, \
463 ptab[i].name, ptab[i].name)
464
465 #define LIDECL(text, tag, test, name) \
466 static void show_##tag(void);
467
468 #define LIDEF(text, tag, test, name) \
469 static void show_##tag(void) \
470 { \
471 unsigned i, w; \
472 LIST(text, stdout, test, name); \
473 }
474
475 #define LIENT(text, tag, test, name) \
476 { #tag, show_##tag },
477
478 struct listent {
479 const char *name;
480 void (*list)(void);
481 };
482
483 #define MAKELISTTAB(listtab, LISTS) \
484 LISTS(LIDECL) \
485 static const struct listent listtab[] = { \
486 LISTS(LIENT) \
487 { 0, 0 } \
488 }; \
489 LISTS(LIDEF)
490
491 extern int displaylists(const struct listent */*listtab*/,
492 char *const /*argv*/[]);
493
494 /*----- Progress indicators -----------------------------------------------*/
495
496 typedef struct fprogress {
497 const char *bp;
498 off_t o, sz, olast;
499 time_t start, last;
500 char name[24];
501 } fprogress;
502
503 /* --- @fprogress_init@ --- *
504 *
505 * Arguments: @fprogress *f@ = progress context to be initialized
506 * @const char *name@ = file name string to show
507 * @FILE *fp@ = file we're reading from
508 *
509 * Returns: Zero on success, nonzero if the file's state is now broken.
510 *
511 * Use: Initializes a progress context. Nothing is actually
512 * displayed yet.
513 */
514
515 extern int fprogress_init(fprogress */*f*/,
516 const char */*name*/, FILE */*fp*/);
517
518 /* --- @fprogress_update@ --- *
519 *
520 * Arguments: @fprogress *f@ = progress context
521 * @size_t n@ = how much progress has been made
522 *
523 * Returns: ---
524 *
525 * Use: Maybe updates the display to show that some progress has been
526 * made.
527 */
528
529 extern void fprogress_update(fprogress */*f*/, size_t /*n*/);
530
531 /* --- @fprogress_clear@ --- *
532 *
533 * Arguments: @fprogress *f@ = progress context
534 *
535 * Returns: ---
536 *
537 * Use: Clears the progress display from the screen.
538 */
539
540 extern void fprogress_clear(fprogress */*f*/);
541
542 /* --- @fprogress_done@ --- *
543 *
544 * Arguments: @fprogress *f@ = progress context
545 *
546 * Returns: ---
547 *
548 * Use: Clear up the progress context and removes any display.
549 */
550
551 extern void fprogress_done(fprogress */*f*/);
552
553 /*----- Subcommand dispatch -----------------------------------------------*/
554
555 typedef struct cmd {
556 const char *name;
557 int (*cmd)(int /*argc*/, char */*argv*/[]);
558 const char *usage;
559 const char *help;
560 } cmd;
561
562 extern void version(FILE */*fp*/);
563 extern void help_global(FILE */*fp*/);
564
565 /* --- @findcmd@ --- *
566 *
567 * Arguments: @const cmd *cmds@ = pointer to command table
568 * @const char *name@ = a command name
569 *
570 * Returns: Pointer to the command structure.
571 *
572 * Use: Looks up a command by name. If the command isn't found, an
573 * error is reported and the program is terminated.
574 */
575
576 const cmd *findcmd(const cmd */*cmds*/, const char */*name*/);
577
578 /* --- @sc_help@ --- *
579 *
580 * Arguments: @const cmd *cmds@ = pointer to command table
581 * @FILE *fp@ = output file handle
582 * @char *const *argv@ = remaining arguments
583 *
584 * Returns: ---
585 *
586 * Use: Prints a help message, maybe with help about subcommands.
587 */
588
589 extern void sc_help(const cmd */*cmds*/, FILE */*fp*/,
590 char *const */*argv*/);
591
592 /*----- That's all, folks -------------------------------------------------*/
593
594 #ifdef __cplusplus
595 }
596 #endif
597
598 #endif