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