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