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