math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / progs / catcrypt.c
1 /* -*-c-*-
2 *
3 * Command-line encryption tool
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include "config.h"
33
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <mLib/base64.h>
40 #include <mLib/dstr.h>
41 #include <mLib/mdwopt.h>
42 #include <mLib/quis.h>
43 #include <mLib/report.h>
44 #include <mLib/sub.h>
45
46 #include "buf.h"
47 #include "rand.h"
48 #include "noise.h"
49 #include "mprand.h"
50 #include "key.h"
51 #include "cc.h"
52
53 #include "ectab.h"
54 #include "ptab.h"
55
56 /*----- Static variables --------------------------------------------------*/
57
58 static const char *keyring = "keyring";
59
60 /*----- Data format -------------------------------------------------------*/
61
62 /* --- Overview --- *
63 *
64 * The encrypted message is divided into chunks, each preceded by a two-octet
65 * length. The chunks don't need to be large -- the idea is that we can
66 * stream the chunks in and out.
67 *
68 * The first chunk is a header. It contains the decryption key-id, and maybe
69 * the verification key-id if the message is signed.
70 *
71 * Next comes the key-encapsulation chunk. This is decrypted in some
72 * KEM-specific way to yield a secret hash. The hash is expanded using an
73 * MGF (or similar) to make a symmetric encryption and MAC key.
74 *
75 * If the message is signed, there comes a signature chunk. The signature is
76 * on the header and key-encapsulation chunks, and further output of the MGF.
77 * This means that the recipient can modify the message and still have a
78 * valid signature, so it's not useful for proving things to other people;
79 * but it also means that the recipient knows that the message is from
80 * someone who knows the hash, which limits the possiblities to (a) whoever
81 * encrypted the message (good!) and (b) whoever knows the recipient's
82 * private key.
83 *
84 * Then come message chunks. Each one begins with a MAC over an implicit
85 * sequence number and the ciphertext. The final chunk's ciphertext is
86 * empty; no other chunk is empty. Thus can the correct end-of-file be
87 * discerned.
88 */
89
90 /*----- Chunk I/O ---------------------------------------------------------*/
91
92 static void chunk_write(enc *e, buf *b)
93 {
94 octet l[2];
95 size_t n = BLEN(b);
96 assert(n <= MASK16);
97 STORE16(l, n);
98 if (e->ops->write(e, l, 2) ||
99 e->ops->write(e, BBASE(b), BLEN(b)))
100 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
101 }
102
103 static void chunk_read(enc *e, dstr *d, buf *b)
104 {
105 octet l[2];
106 size_t n;
107
108 dstr_reset(d);
109 errno = 0;
110 if (e->ops->read(e, l, 2) != 2)
111 goto err;
112 n = LOAD16(l);
113 dstr_ensure(d, n);
114 if (e->ops->read(e, d->buf, n) != n)
115 goto err;
116 d->len = n;
117 buf_init(b, d->buf, d->len);
118 return;
119
120 err:
121 if (!errno) die(EXIT_FAILURE, "unexpected end-of-file on input");
122 else die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
123 }
124
125 /*----- Encryption --------------------------------------------------------*/
126
127 static int encrypt(int argc, char *argv[])
128 {
129 const char *fn, *of = 0, *kn = "ccrypt", *skn = 0;
130 FILE *ofp = 0;
131 FILE *fp = 0;
132 const char *ef = "binary";
133 fprogress ff;
134 const char *err;
135 int i;
136 int en;
137 size_t n, chsz;
138 dstr d = DSTR_INIT;
139 octet *tag, *ct;
140 buf b;
141 size_t seq;
142 char bb[65536];
143 unsigned f = 0;
144 key_file kf;
145 key *k;
146 key *sk = 0;
147 kem *km;
148 sig *s = 0;
149 gcipher *cx, *c;
150 gmac *m;
151 ghash *h;
152 const encops *eo;
153 enc *e;
154
155 #define f_bogus 1u
156 #define f_nocheck 2u
157 #define f_progress 4u
158
159 for (;;) {
160 static const struct option opt[] = {
161 { "key", OPTF_ARGREQ, 0, 'k' },
162 { "sign-key", OPTF_ARGREQ, 0, 's' },
163 { "armour", 0, 0, 'a' },
164 { "armor", 0, 0, 'a' },
165 { "format", OPTF_ARGREQ, 0, 'f' },
166 { "output", OPTF_ARGREQ, 0, 'o' },
167 { "progress", 0, 0, 'p' },
168 { "nocheck", 0, 0, 'C' },
169 { 0, 0, 0, 0 }
170 };
171 i = mdwopt(argc, argv, "k:s:af:o:pC", opt, 0, 0, 0);
172 if (i < 0) break;
173 switch (i) {
174 case 'k': kn = optarg; break;
175 case 's': skn = optarg; break;
176 case 'a': ef = "pem"; break;
177 case 'f': ef = optarg; break;
178 case 'o': of = optarg; break;
179 case 'p': f |= f_progress; break;
180 case 'C': f |= f_nocheck; break;
181 default: f |= f_bogus; break;
182 }
183 }
184 if (argc - optind > 1 || (f & f_bogus))
185 die(EXIT_FAILURE, "Usage: encrypt [-OPTIONS] [FILE]");
186
187 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
188 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
189 if ((k = key_bytag(&kf, kn)) == 0)
190 die(EXIT_FAILURE, "key `%s' not found", kn);
191 if (skn && (sk = key_bytag(&kf, skn)) == 0)
192 die(EXIT_FAILURE, "key `%s' not found", skn);
193
194 if ((eo = getenc(ef)) == 0)
195 die(EXIT_FAILURE, "encoding `%s' not found", ef);
196
197 fn = optind < argc ? argv[optind++] : "-";
198 if (strcmp(fn, "-") == 0)
199 fp = stdin;
200 else if ((fp = fopen(fn, "rb")) == 0) {
201 die(EXIT_FAILURE, "couldn't open file `%s': %s",
202 fn, strerror(errno));
203 }
204
205 if (!of || strcmp(of, "-") == 0)
206 ofp = stdout;
207 else if ((ofp = fopen(of, eo->wmode)) == 0) {
208 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
209 of, strerror(errno));
210 }
211
212 dstr_reset(&d);
213 key_fulltag(k, &d);
214 e = initenc(eo, ofp, "CATCRYPT ENCRYPTED MESSAGE");
215 km = getkem(k, "cckem", 0);
216 if (!(f & f_nocheck) && (err = km->ops->check(km)) != 0)
217 moan("key %s fails check: %s", d.buf, err);
218 if (sk) {
219 dstr_reset(&d);
220 key_fulltag(sk, &d);
221 s = getsig(sk, "ccsig", 1);
222 if ((err = s->ops->check(s)) != 0)
223 moan("key %s fails check: %s", d.buf, err);
224 }
225
226 /* --- Build the header chunk --- */
227
228 dstr_reset(&d);
229 dstr_ensure(&d, 256);
230 buf_init(&b, d.buf, 256);
231 buf_putu32(&b, k->id);
232 if (sk) buf_putu32(&b, sk->id);
233 assert(BOK(&b));
234 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
235 chunk_write(e, &b);
236
237 /* --- Build the KEM chunk --- */
238
239 dstr_reset(&d);
240 if (setupkem(km, &d, &cx, &c, &m))
241 die(EXIT_FAILURE, "failed to encapsulate key");
242 buf_init(&b, d.buf, d.len);
243 BSTEP(&b, d.len);
244 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
245 chunk_write(e, &b);
246
247 /* --- Write the signature chunk --- */
248
249 if (s) {
250 GC_ENCRYPT(cx, 0, bb, 1024);
251 GH_HASH(s->h, bb, 1024);
252 dstr_reset(&d);
253 if ((en = s->ops->doit(s, &d)) != 0)
254 die(EXIT_FAILURE, "error creating signature: %s", key_strerror(en));
255 buf_init(&b, d.buf, d.len);
256 BSTEP(&b, d.len);
257 chunk_write(e, &b);
258 }
259
260 /* --- Now do the main crypto --- */
261
262 if (f & f_progress) {
263 if (fprogress_init(&ff, fn, fp)) {
264 die(EXIT_FAILURE, "failed to initialize progress display: %s",
265 strerror(errno));
266 }
267 }
268
269 assert(GC_CLASS(c)->blksz <= sizeof(bb));
270 dstr_ensure(&d, sizeof(bb) + GM_CLASS(m)->hashsz);
271 seq = 0;
272 chsz = MASK16 - GM_CLASS(m)->hashsz;
273 for (;;) {
274 h = GM_INIT(m);
275 GH_HASHU32(h, seq);
276 seq++;
277 if (GC_CLASS(c)->blksz) {
278 GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz);
279 GC_SETIV(c, bb);
280 }
281 n = fread(bb, 1, chsz, fp);
282 if (!n) break;
283 if (f & f_progress) fprogress_update(&ff, n);
284 buf_init(&b, d.buf, d.sz);
285 tag = buf_get(&b, GM_CLASS(m)->hashsz);
286 ct = buf_get(&b, n);
287 assert(tag); assert(ct);
288 GC_ENCRYPT(c, bb, ct, n);
289 GH_HASH(h, ct, n);
290 GH_DONE(h, tag);
291 GH_DESTROY(h);
292 chunk_write(e, &b);
293 }
294
295 /* --- Final terminator packet --- */
296
297 buf_init(&b, d.buf, d.sz);
298 tag = buf_get(&b, GM_CLASS(m)->hashsz);
299 assert(tag);
300 GH_DONE(h, tag);
301 GH_DESTROY(h);
302 chunk_write(e, &b);
303
304 /* --- All done --- */
305
306 if (f & f_progress) fprogress_done(&ff);
307 e->ops->encdone(e);
308 GM_DESTROY(m);
309 GC_DESTROY(c);
310 GC_DESTROY(cx);
311 freeenc(e);
312 if (s) freesig(s);
313 freekem(km);
314 if (fp != stdin) fclose(fp);
315 if (of) fclose(ofp);
316 key_close(&kf);
317 dstr_destroy(&d);
318 return (0);
319
320 #undef f_bogus
321 #undef f_nocheck
322 #undef f_progress
323 }
324
325 /*---- Decryption ---------------------------------------------------------*/
326
327 static int decrypt(int argc, char *argv[])
328 {
329 const char *fn, *of = 0;
330 FILE *ofp = 0, *rfp = 0;
331 FILE *fp = 0;
332 const char *ef = "binary";
333 fprogress ff;
334 int i;
335 size_t n;
336 dstr d = DSTR_INIT;
337 buf b;
338 key_file kf;
339 size_t seq;
340 uint32 id;
341 key *k;
342 key *sk = 0;
343 kem *km;
344 sig *s = 0;
345 gcipher *cx;
346 gcipher *c;
347 ghash *h;
348 gmac *m;
349 octet *tag;
350 unsigned f = 0;
351 const encops *eo;
352 const char *err;
353 int verb = 1;
354 enc *e;
355
356 #define f_bogus 1u
357 #define f_buffer 2u
358 #define f_nocheck 4u
359 #define f_progress 8u
360
361 for (;;) {
362 static const struct option opt[] = {
363 { "armour", 0, 0, 'a' },
364 { "armor", 0, 0, 'a' },
365 { "buffer", 0, 0, 'b' },
366 { "verbose", 0, 0, 'v' },
367 { "quiet", 0, 0, 'q' },
368 { "nocheck", 0, 0, 'C' },
369 { "format", OPTF_ARGREQ, 0, 'f' },
370 { "output", OPTF_ARGREQ, 0, 'o' },
371 { "progress", 0, 0, 'p' },
372 { 0, 0, 0, 0 }
373 };
374 i = mdwopt(argc, argv, "abf:o:pqvC", opt, 0, 0, 0);
375 if (i < 0) break;
376 switch (i) {
377 case 'a': ef = "pem"; break;
378 case 'b': f |= f_buffer; break;
379 case 'v': verb++; break;
380 case 'q': if (verb) verb--; break;
381 case 'C': f |= f_nocheck; break;
382 case 'f': ef = optarg; break;
383 case 'o': of = optarg; break;
384 case 'p': f |= f_progress; break;
385 default: f |= f_bogus; break;
386 }
387 }
388 if (argc - optind > 1 || (f & f_bogus))
389 die(EXIT_FAILURE, "Usage: decrypt [-OPTIONS] [FILE]");
390
391 if ((eo = getenc(ef)) == 0)
392 die(EXIT_FAILURE, "encoding `%s' not found", ef);
393
394 fn = optind < argc ? argv[optind++] : "-";
395 if (strcmp(fn, "-") == 0)
396 fp = stdin;
397 else if ((fp = fopen(fn, eo->rmode)) == 0) {
398 die(EXIT_FAILURE, "couldn't open file `%s': %s",
399 fn, strerror(errno));
400 }
401
402 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
403 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
404
405 e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE");
406
407 if (f & f_progress) {
408 if (fprogress_init(&ff, fn, fp)) {
409 die(EXIT_FAILURE, "failed to initialize progress display: %s",
410 strerror(errno));
411 }
412 }
413
414 /* --- Read the header chunk --- */
415
416 chunk_read(e, &d, &b);
417 if (f & f_progress)
418 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
419 if (buf_getu32(&b, &id)) {
420 if (f & f_progress) fprogress_done(&ff);
421 if (verb) printf("FAIL malformed header: missing keyid\n");
422 exit(EXIT_FAILURE);
423 }
424 if ((k = key_byid(&kf, id)) == 0) {
425 if (f & f_progress) fprogress_done(&ff);
426 if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id);
427 exit(EXIT_FAILURE);
428 }
429 if (BLEFT(&b)) {
430 if (buf_getu32(&b, &id)) {
431 if (f & f_progress) fprogress_done(&ff);
432 if (verb) printf("FAIL malformed header: missing signature keyid\n");
433 exit(EXIT_FAILURE);
434 }
435 if ((sk = key_byid(&kf, id)) == 0) {
436 if (f & f_progress) fprogress_done(&ff);
437 if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id);
438 exit(EXIT_FAILURE);
439 }
440 }
441 if (BLEFT(&b)) {
442 if (f & f_progress) fprogress_done(&ff);
443 if (verb) printf("FAIL malformed header: junk at end\n");
444 exit(EXIT_FAILURE);
445 }
446 if (sk) {
447 s = getsig(sk, "ccsig", 0);
448 if (!(f & f_nocheck) && verb && (err = s->ops->check(s)) != 0) {
449 dstr_reset(&d);
450 key_fulltag(sk, &d);
451 printf("WARN verification key %s fails check: %s\n", d.buf, err);
452 }
453 GH_HASHBUF16(s->h, BBASE(&b), BSZ(&b));
454 }
455
456 /* --- Find the key --- */
457
458 km = getkem(k, "cckem", 1);
459
460 /* --- Read the KEM chunk --- */
461
462 chunk_read(e, &d, &b);
463 if (f & f_progress)
464 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
465 if (setupkem(km, &d, &cx, &c, &m)) {
466 if (f & f_progress) fprogress_done(&ff);
467 if (verb) printf("FAIL failed to decapsulate key\n");
468 exit(EXIT_FAILURE);
469 }
470 if (s) GH_HASHBUF16(s->h, d.buf, d.len);
471
472 /* --- Verify the signature, if there is one --- */
473
474 if (sk) {
475 dstr_reset(&d);
476 dstr_ensure(&d, 1024);
477 GC_ENCRYPT(cx, 0, d.buf, 1024);
478 GH_HASH(s->h, d.buf, 1024);
479 chunk_read(e, &d, &b);
480 if (f & f_progress)
481 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
482 if (s->ops->doit(s, &d)) {
483 if (f & f_progress) fprogress_done(&ff);
484 if (verb) printf("FAIL signature verification failed\n");
485 exit(EXIT_FAILURE);
486 }
487 if (verb) {
488 dstr_reset(&d);
489 key_fulltag(sk, &d);
490 if (f & f_progress) fprogress_clear(&ff);
491 printf("INFO good-signature %s\n", d.buf);
492 }
493 freesig(s);
494 } else if (verb) {
495 if (f & f_progress) fprogress_clear(&ff);
496 printf("INFO no-signature\n");
497 }
498
499 /* --- Now decrypt the main body --- */
500
501 if (!of || strcmp(of, "-") == 0) {
502 ofp = stdout;
503 f |= f_buffer;
504 }
505 if (!(f & f_buffer)) {
506 if ((ofp = fopen(of, "wb")) == 0) {
507 if (f & f_progress) fprogress_done(&ff);
508 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
509 of, strerror(errno));
510 }
511 rfp = ofp;
512 } else if ((rfp = tmpfile()) == 0) {
513 if (f & f_progress) fprogress_done(&ff);
514 die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno));
515 }
516
517 seq = 0;
518 dstr_ensure(&d, GC_CLASS(c)->blksz);
519 dstr_ensure(&d, 4);
520 for (;;) {
521 if (GC_CLASS(c)->blksz) {
522 GC_ENCRYPT(cx, 0, d.buf, GC_CLASS(c)->blksz);
523 GC_SETIV(c, d.buf);
524 }
525 h = GM_INIT(m);
526 GH_HASHU32(h, seq);
527 seq++;
528 chunk_read(e, &d, &b);
529 if (f & f_progress)
530 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
531 if ((tag = buf_get(&b, GM_CLASS(m)->hashsz)) == 0) {
532 if (f & f_progress) fprogress_done(&ff);
533 if (verb) printf("FAIL bad ciphertext chunk: no tag\n");
534 exit(EXIT_FAILURE);
535 }
536 GH_HASH(h, BCUR(&b), BLEFT(&b));
537 if (memcmp(tag, GH_DONE(h, 0), GM_CLASS(m)->hashsz) != 0) {
538 if (f & f_progress) fprogress_done(&ff);
539 if (verb)
540 printf("FAIL bad ciphertext chunk: authentication failure\n");
541 exit(EXIT_FAILURE);
542 }
543 GH_DESTROY(h);
544 if (!BLEFT(&b))
545 break;
546 GC_DECRYPT(c, BCUR(&b), BCUR(&b), BLEFT(&b));
547 if (fwrite(BCUR(&b), 1, BLEFT(&b), rfp) != BLEFT(&b)) {
548 if (f & f_progress) fprogress_done(&ff);
549 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
550 exit(EXIT_FAILURE);
551 }
552 }
553
554 if (f & f_progress) fprogress_done(&ff);
555 if (fflush(rfp) || ferror(rfp)) {
556 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
557 exit(EXIT_FAILURE);
558 }
559 if (f & f_buffer) {
560 if (!ofp && (ofp = fopen(of, "wb")) == 0) {
561 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
562 of, strerror(errno));
563 }
564 rewind(rfp);
565 if (f & f_progress) fprogress_init(&ff, "copying buffer", rfp);
566 dstr_reset(&d);
567 dstr_ensure(&d, 65536);
568 if (verb && ofp == stdout) printf("DATA\n");
569 for (;;) {
570 n = fread(d.buf, 1, d.sz, rfp);
571 if (!n) break;
572 if (f & f_progress) fprogress_update(&ff, n);
573 if (fwrite(d.buf, 1, n, ofp) < n) {
574 if (f & f_progress) fprogress_done(&ff);
575 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
576 }
577 }
578 if (f & f_progress) fprogress_done(&ff);
579 if (ferror(rfp) || fclose(rfp))
580 die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno));
581 }
582
583 e->ops->decdone(e);
584 if (verb && ofp != stdout)
585 printf("OK decrypted successfully\n");
586 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
587 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
588 freeenc(e);
589 GC_DESTROY(c);
590 GC_DESTROY(cx);
591 GM_DESTROY(m);
592 freekem(km);
593 if (fp != stdin) fclose(fp);
594 key_close(&kf);
595 dstr_destroy(&d);
596 return (0);
597
598 #undef f_bogus
599 #undef f_buffer
600 #undef f_nocheck
601 #undef f_progress
602 }
603
604 /*----- Main code ---------------------------------------------------------*/
605
606 #define LISTS(LI) \
607 LI("Lists", list, \
608 listtab[i].name, listtab[i].name) \
609 LI("Key-encapsulation mechanisms", kem, \
610 kemtab[i].name, kemtab[i].name) \
611 LI("Signature schemes", sig, \
612 sigtab[i].name, sigtab[i].name) \
613 LI("Encodings", enc, \
614 enctab[i].name, enctab[i].name) \
615 LI("Symmetric encryption algorithms", cipher, \
616 gciphertab[i], gciphertab[i]->name) \
617 LI("Hash functions", hash, \
618 ghashtab[i], ghashtab[i]->name) \
619 LI("Message authentication codes", mac, \
620 gmactab[i], gmactab[i]->name)
621
622 MAKELISTTAB(listtab, LISTS)
623
624 int cmd_show(int argc, char *argv[])
625 {
626 return (displaylists(listtab, argv + 1));
627 }
628
629 static int cmd_help(int, char **);
630
631 static cmd cmdtab[] = {
632 { "help", cmd_help, "help [COMMAND...]" },
633 { "show", cmd_show, "show [ITEM...]" },
634 CMD_ENCODE,
635 CMD_DECODE,
636 { "encrypt", encrypt,
637 "encrypt [-apC] [-k TAG] [-s TAG] [-f FORMAT]\n\t\
638 [-o OUTPUT] [FILE]", "\
639 Options:\n\
640 \n\
641 -a, --armour Same as `-f pem'.\n\
642 -f, --format=FORMAT Encode as FORMAT.\n\
643 -k, --key=TAG Use public encryption key named by TAG.\n\
644 -s, --sign-key=TAG Use private signature key named by TAG.\n\
645 -o, --output=FILE Write output to FILE.\n\
646 -p, --progress Show progress on large files.\n\
647 -C, --nocheck Don't check the public key.\n\
648 " },
649 { "decrypt", decrypt,
650 "decrypt [-abpqvC] [-f FORMAT] [-o OUTPUT] [FILE]", "\
651 Options:\n\
652 \n\
653 -a, --armour Same as `-f pem'.\n\
654 -b, --buffer Buffer output until we're sure we have it all.\n\
655 -f, --format=FORMAT Decode as FORMAT.\n\
656 -o, --output=FILE Write output to FILE.\n\
657 -p, --progress Show progress on large files.\n\
658 -q, --quiet Produce fewer messages.\n\
659 -v, --verbose Produce more verbose messages.\n\
660 -C, --nocheck Don't check the private key.\n\
661 " },
662 { 0, 0, 0 }
663 };
664
665 static int cmd_help(int argc, char **argv)
666 {
667 sc_help(cmdtab, stdout, argv + 1);
668 return (0);
669 }
670
671 void version(FILE *fp)
672 {
673 pquis(fp, "$, Catacomb version " VERSION "\n");
674 }
675
676 static void usage(FILE *fp)
677 {
678 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
679 }
680
681 void help_global(FILE *fp)
682 {
683 usage(fp);
684 fputs("\n\
685 Encrypt and decrypt files.\n\
686 \n\
687 Global command-line options:\n\
688 \n\
689 -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\
690 -v, --version Show program version number.\n\
691 -u, --usage Show a terse usage message.\n\
692 \n\
693 -k, --keyring=FILE Read keys from FILE.\n",
694 fp);
695 }
696
697 /* --- @main@ --- *
698 *
699 * Arguments: @int argc@ = number of command line arguments
700 * @char *argv[]@ = vector of command line arguments
701 *
702 * Returns: Zero if successful, nonzero otherwise.
703 *
704 * Use: Encrypts or decrypts files.
705 */
706
707 int main(int argc, char *argv[])
708 {
709 unsigned f = 0;
710
711 #define f_bogus 1u
712
713 /* --- Initialize the library --- */
714
715 ego(argv[0]);
716 sub_init();
717 rand_noisesrc(RAND_GLOBAL, &noise_source);
718 rand_seed(RAND_GLOBAL, 160);
719
720 /* --- Parse options --- */
721
722 for (;;) {
723 static struct option opts[] = {
724 { "help", 0, 0, 'h' },
725 { "version", 0, 0, 'v' },
726 { "usage", 0, 0, 'u' },
727 { "keyring", OPTF_ARGREQ, 0, 'k' },
728 { 0, 0, 0, 0 }
729 };
730 int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0);
731 if (i < 0)
732 break;
733 switch (i) {
734 case 'h':
735 sc_help(cmdtab, stdout, argv + optind);
736 exit(0);
737 break;
738 case 'v':
739 version(stdout);
740 exit(0);
741 break;
742 case 'u':
743 usage(stdout);
744 exit(0);
745 case 'k':
746 keyring = optarg;
747 break;
748 default:
749 f |= f_bogus;
750 break;
751 }
752 }
753
754 argc -= optind;
755 argv += optind;
756 optind = 0;
757 if (f & f_bogus || argc < 1) {
758 usage(stderr);
759 exit(EXIT_FAILURE);
760 }
761
762 /* --- Dispatch to the correct subcommand handler --- */
763
764 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
765
766 #undef f_bogus
767 }
768
769 /*----- That's all, folks -------------------------------------------------*/