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