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