progs/catcrypt.c, progs/cc-sig.c: Compare MAC tags in constant time.
[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;
140 octet *tag, *ct;
141 buf b;
142 size_t seq;
188f51de 143 char bb[65536];
5c3f75ec 144 unsigned f = 0;
145 key_file kf;
146 key *k;
c65df279 147 key *sk = 0;
5c3f75ec 148 kem *km;
c65df279 149 sig *s = 0;
5c3f75ec 150 gcipher *cx, *c;
151 gmac *m;
152 ghash *h;
153 const encops *eo;
154 enc *e;
155
156#define f_bogus 1u
946c3f72 157#define f_nocheck 2u
cd6eca43 158#define f_progress 4u
5c3f75ec 159
160 for (;;) {
161 static const struct option opt[] = {
162 { "key", OPTF_ARGREQ, 0, 'k' },
c65df279 163 { "sign-key", OPTF_ARGREQ, 0, 's' },
5c3f75ec 164 { "armour", 0, 0, 'a' },
165 { "armor", 0, 0, 'a' },
45c0fd36
MW
166 { "format", OPTF_ARGREQ, 0, 'f' },
167 { "output", OPTF_ARGREQ, 0, 'o' },
cd6eca43 168 { "progress", 0, 0, 'p' },
946c3f72 169 { "nocheck", 0, 0, 'C' },
5c3f75ec 170 { 0, 0, 0, 0 }
171 };
cd6eca43 172 i = mdwopt(argc, argv, "k:s:af:o:pC", opt, 0, 0, 0);
5c3f75ec 173 if (i < 0) break;
174 switch (i) {
175 case 'k': kn = optarg; break;
c65df279 176 case 's': skn = optarg; break;
5c3f75ec 177 case 'a': ef = "pem"; break;
178 case 'f': ef = optarg; break;
179 case 'o': of = optarg; break;
cd6eca43 180 case 'p': f |= f_progress; break;
946c3f72 181 case 'C': f |= f_nocheck; break;
5c3f75ec 182 default: f |= f_bogus; break;
183 }
184 }
185 if (argc - optind > 1 || (f & f_bogus))
c65df279 186 die(EXIT_FAILURE, "Usage: encrypt [-OPTIONS] [FILE]");
5c3f75ec 187
213e565f 188 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
5c3f75ec 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);
c65df279 192 if (skn && (sk = key_bytag(&kf, skn)) == 0)
193 die(EXIT_FAILURE, "key `%s' not found", skn);
5c3f75ec 194
195 if ((eo = getenc(ef)) == 0)
196 die(EXIT_FAILURE, "encoding `%s' not found", ef);
197
cd6eca43
MW
198 fn = optind < argc ? argv[optind++] : "-";
199 if (strcmp(fn, "-") == 0)
5c3f75ec 200 fp = stdin;
cd6eca43 201 else if ((fp = fopen(fn, "rb")) == 0) {
5c3f75ec 202 die(EXIT_FAILURE, "couldn't open file `%s': %s",
cd6eca43
MW
203 fn, strerror(errno));
204 }
5c3f75ec 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",
9b1663a5 210 of, strerror(errno));
5c3f75ec 211 }
212
c65df279 213 dstr_reset(&d);
5c3f75ec 214 key_fulltag(k, &d);
fa54fe1e 215 e = initenc(eo, ofp, "CATCRYPT ENCRYPTED MESSAGE");
c65df279 216 km = getkem(k, "cckem", 0);
946c3f72 217 if (!(f & f_nocheck) && (err = km->ops->check(km)) != 0)
fa54fe1e 218 moan("key %s fails check: %s", d.buf, err);
c65df279 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)
fa54fe1e 224 moan("key %s fails check: %s", d.buf, err);
c65df279 225 }
5c3f75ec 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);
c65df279 233 if (sk) buf_putu32(&b, sk->id);
5c3f75ec 234 assert(BOK(&b));
f9e51332 235 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
5c3f75ec 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);
f9e51332 245 if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b));
5c3f75ec 246 chunk_write(e, &b);
247
c65df279 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);
45c0fd36 259 }
c65df279 260
5c3f75ec 261 /* --- Now do the main crypto --- */
262
cd6eca43
MW
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
5c3f75ec 270 assert(GC_CLASS(c)->blksz <= sizeof(bb));
271 dstr_ensure(&d, sizeof(bb) + GM_CLASS(m)->hashsz);
272 seq = 0;
f52f2db0 273 chsz = MASK16 - GM_CLASS(m)->hashsz;
5c3f75ec 274 for (;;) {
275 h = GM_INIT(m);
dfd1608a 276 GH_HASHU32(h, seq);
5c3f75ec 277 seq++;
278 if (GC_CLASS(c)->blksz) {
279 GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz);
280 GC_SETIV(c, bb);
281 }
f52f2db0 282 n = fread(bb, 1, chsz, fp);
5c3f75ec 283 if (!n) break;
cd6eca43 284 if (f & f_progress) fprogress_update(&ff, n);
5c3f75ec 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
cd6eca43 307 if (f & f_progress) fprogress_done(&ff);
5c3f75ec 308 e->ops->encdone(e);
309 GM_DESTROY(m);
310 GC_DESTROY(c);
311 GC_DESTROY(cx);
312 freeenc(e);
c65df279 313 if (s) freesig(s);
5c3f75ec 314 freekem(km);
ef13e9a4 315 if (fp != stdin) fclose(fp);
5c3f75ec 316 if (of) fclose(ofp);
317 key_close(&kf);
318 dstr_destroy(&d);
319 return (0);
320
321#undef f_bogus
946c3f72 322#undef f_nocheck
cd6eca43 323#undef f_progress
5c3f75ec 324}
325
326/*---- Decryption ---------------------------------------------------------*/
327
328static int decrypt(int argc, char *argv[])
329{
cd6eca43 330 const char *fn, *of = 0;
fa54fe1e 331 FILE *ofp = 0, *rfp = 0;
5c3f75ec 332 FILE *fp = 0;
333 const char *ef = "binary";
cd6eca43 334 fprogress ff;
5c3f75ec 335 int i;
fa54fe1e 336 size_t n;
5c3f75ec 337 dstr d = DSTR_INIT;
338 buf b;
339 key_file kf;
340 size_t seq;
341 uint32 id;
342 key *k;
c65df279 343 key *sk = 0;
5c3f75ec 344 kem *km;
c65df279 345 sig *s = 0;
5c3f75ec 346 gcipher *cx;
347 gcipher *c;
348 ghash *h;
349 gmac *m;
350 octet *tag;
351 unsigned f = 0;
352 const encops *eo;
c65df279 353 const char *err;
354 int verb = 1;
5c3f75ec 355 enc *e;
356
357#define f_bogus 1u
fa54fe1e 358#define f_buffer 2u
946c3f72 359#define f_nocheck 4u
cd6eca43 360#define f_progress 8u
5c3f75ec 361
362 for (;;) {
363 static const struct option opt[] = {
364 { "armour", 0, 0, 'a' },
365 { "armor", 0, 0, 'a' },
fa54fe1e 366 { "buffer", 0, 0, 'b' },
c65df279 367 { "verbose", 0, 0, 'v' },
368 { "quiet", 0, 0, 'q' },
946c3f72 369 { "nocheck", 0, 0, 'C' },
45c0fd36
MW
370 { "format", OPTF_ARGREQ, 0, 'f' },
371 { "output", OPTF_ARGREQ, 0, 'o' },
cd6eca43 372 { "progress", 0, 0, 'p' },
5c3f75ec 373 { 0, 0, 0, 0 }
374 };
cd6eca43 375 i = mdwopt(argc, argv, "abf:o:pqvC", opt, 0, 0, 0);
5c3f75ec 376 if (i < 0) break;
377 switch (i) {
378 case 'a': ef = "pem"; break;
fa54fe1e 379 case 'b': f |= f_buffer; break;
c65df279 380 case 'v': verb++; break;
381 case 'q': if (verb) verb--; break;
946c3f72 382 case 'C': f |= f_nocheck; break;
5c3f75ec 383 case 'f': ef = optarg; break;
384 case 'o': of = optarg; break;
cd6eca43 385 case 'p': f |= f_progress; break;
5c3f75ec 386 default: f |= f_bogus; break;
387 }
388 }
389 if (argc - optind > 1 || (f & f_bogus))
c65df279 390 die(EXIT_FAILURE, "Usage: decrypt [-OPTIONS] [FILE]");
5c3f75ec 391
392 if ((eo = getenc(ef)) == 0)
393 die(EXIT_FAILURE, "encoding `%s' not found", ef);
394
cd6eca43
MW
395 fn = optind < argc ? argv[optind++] : "-";
396 if (strcmp(fn, "-") == 0)
5c3f75ec 397 fp = stdin;
cd6eca43 398 else if ((fp = fopen(fn, eo->rmode)) == 0) {
5c3f75ec 399 die(EXIT_FAILURE, "couldn't open file `%s': %s",
cd6eca43
MW
400 fn, strerror(errno));
401 }
5c3f75ec 402
213e565f 403 if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0))
5c3f75ec 404 die(EXIT_FAILURE, "can't open keyring `%s'", keyring);
405
fa54fe1e 406 e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE");
5c3f75ec 407
cd6eca43
MW
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
5c3f75ec 415 /* --- Read the header chunk --- */
416
417 chunk_read(e, &d, &b);
cd6eca43
MW
418 if (f & f_progress)
419 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
c65df279 420 if (buf_getu32(&b, &id)) {
cd6eca43 421 if (f & f_progress) fprogress_done(&ff);
c65df279 422 if (verb) printf("FAIL malformed header: missing keyid\n");
423 exit(EXIT_FAILURE);
424 }
425 if ((k = key_byid(&kf, id)) == 0) {
cd6eca43 426 if (f & f_progress) fprogress_done(&ff);
c65df279 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)) {
cd6eca43 432 if (f & f_progress) fprogress_done(&ff);
c65df279 433 if (verb) printf("FAIL malformed header: missing signature keyid\n");
434 exit(EXIT_FAILURE);
435 }
436 if ((sk = key_byid(&kf, id)) == 0) {
cd6eca43 437 if (f & f_progress) fprogress_done(&ff);
c65df279 438 if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id);
439 exit(EXIT_FAILURE);
440 }
441 }
442 if (BLEFT(&b)) {
cd6eca43 443 if (f & f_progress) fprogress_done(&ff);
c65df279 444 if (verb) printf("FAIL malformed header: junk at end\n");
445 exit(EXIT_FAILURE);
446 }
f9e51332 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 }
5c3f75ec 456
457 /* --- Find the key --- */
458
c65df279 459 km = getkem(k, "cckem", 1);
5c3f75ec 460
461 /* --- Read the KEM chunk --- */
462
463 chunk_read(e, &d, &b);
cd6eca43
MW
464 if (f & f_progress)
465 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
c65df279 466 if (setupkem(km, &d, &cx, &c, &m)) {
cd6eca43 467 if (f & f_progress) fprogress_done(&ff);
c65df279 468 if (verb) printf("FAIL failed to decapsulate key\n");
469 exit(EXIT_FAILURE);
470 }
f9e51332 471 if (s) GH_HASHBUF16(s->h, d.buf, d.len);
c65df279 472
473 /* --- Verify the signature, if there is one --- */
474
475 if (sk) {
c65df279 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);
cd6eca43
MW
481 if (f & f_progress)
482 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
c65df279 483 if (s->ops->doit(s, &d)) {
cd6eca43 484 if (f & f_progress) fprogress_done(&ff);
c65df279 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);
cd6eca43 491 if (f & f_progress) fprogress_clear(&ff);
fa54fe1e 492 printf("INFO good-signature %s\n", d.buf);
c65df279 493 }
ef13e9a4 494 freesig(s);
cd6eca43
MW
495 } else if (verb) {
496 if (f & f_progress) fprogress_clear(&ff);
fa54fe1e 497 printf("INFO no-signature\n");
cd6eca43 498 }
5c3f75ec 499
500 /* --- Now decrypt the main body --- */
501
c65df279 502 if (!of || strcmp(of, "-") == 0) {
5c3f75ec 503 ofp = stdout;
fa54fe1e 504 f |= f_buffer;
5c3f75ec 505 }
fa54fe1e 506 if (!(f & f_buffer)) {
507 if ((ofp = fopen(of, "wb")) == 0) {
cd6eca43 508 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 509 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
9b1663a5 510 of, strerror(errno));
fa54fe1e 511 }
512 rfp = ofp;
cd6eca43
MW
513 } else if ((rfp = tmpfile()) == 0) {
514 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 515 die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno));
cd6eca43 516 }
5c3f75ec 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);
dfd1608a 527 GH_HASHU32(h, seq);
5c3f75ec 528 seq++;
529 chunk_read(e, &d, &b);
cd6eca43
MW
530 if (f & f_progress)
531 fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw);
c65df279 532 if ((tag = buf_get(&b, GM_CLASS(m)->hashsz)) == 0) {
cd6eca43 533 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 534 if (verb) printf("FAIL bad ciphertext chunk: no tag\n");
c65df279 535 exit(EXIT_FAILURE);
536 }
5c3f75ec 537 GH_HASH(h, BCUR(&b), BLEFT(&b));
da4489a6 538 if (!ct_memeq(tag, GH_DONE(h, 0), GM_CLASS(m)->hashsz)) {
cd6eca43 539 if (f & f_progress) fprogress_done(&ff);
c65df279 540 if (verb)
fa54fe1e 541 printf("FAIL bad ciphertext chunk: authentication failure\n");
c65df279 542 exit(EXIT_FAILURE);
543 }
ef13e9a4 544 GH_DESTROY(h);
5c3f75ec 545 if (!BLEFT(&b))
546 break;
547 GC_DECRYPT(c, BCUR(&b), BCUR(&b), BLEFT(&b));
fa54fe1e 548 if (fwrite(BCUR(&b), 1, BLEFT(&b), rfp) != BLEFT(&b)) {
cd6eca43 549 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 550 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
c65df279 551 exit(EXIT_FAILURE);
552 }
553 }
554
cd6eca43 555 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 556 if (fflush(rfp) || ferror(rfp)) {
557 if (verb) printf("FAIL error writing output: %s\n", strerror(errno));
c65df279 558 exit(EXIT_FAILURE);
5c3f75ec 559 }
fa54fe1e 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);
cd6eca43 566 if (f & f_progress) fprogress_init(&ff, "copying buffer", rfp);
fa54fe1e 567 dstr_reset(&d);
568 dstr_ensure(&d, 65536);
64459c98 569 if (verb && ofp == stdout) printf("DATA\n");
fa54fe1e 570 for (;;) {
571 n = fread(d.buf, 1, d.sz, rfp);
572 if (!n) break;
cd6eca43
MW
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);
fa54fe1e 576 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
cd6eca43 577 }
fa54fe1e 578 }
cd6eca43 579 if (f & f_progress) fprogress_done(&ff);
fa54fe1e 580 if (ferror(rfp) || fclose(rfp))
581 die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno));
582 }
5c3f75ec 583
5c3f75ec 584 e->ops->decdone(e);
c65df279 585 if (verb && ofp != stdout)
586 printf("OK decrypted successfully\n");
f0c52873
MW
587 if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp)))
588 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
5c3f75ec 589 freeenc(e);
590 GC_DESTROY(c);
591 GC_DESTROY(cx);
592 GM_DESTROY(m);
593 freekem(km);
ef13e9a4 594 if (fp != stdin) fclose(fp);
5c3f75ec 595 key_close(&kf);
596 dstr_destroy(&d);
597 return (0);
598
599#undef f_bogus
fa54fe1e 600#undef f_buffer
946c3f72 601#undef f_nocheck
cd6eca43 602#undef f_progress
5c3f75ec 603}
604
605/*----- Main code ---------------------------------------------------------*/
606
c65df279 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
623MAKELISTTAB(listtab, LISTS)
624
625int cmd_show(int argc, char *argv[])
626{
627 return (displaylists(listtab, argv + 1));
628}
629
630static int cmd_help(int, char **);
5c3f75ec 631
632static cmd cmdtab[] = {
c65df279 633 { "help", cmd_help, "help [COMMAND...]" },
634 { "show", cmd_show, "show [ITEM...]" },
fa54fe1e 635 CMD_ENCODE,
636 CMD_DECODE,
5c3f75ec 637 { "encrypt", encrypt,
cd6eca43 638 "encrypt [-apC] [-k TAG] [-s TAG] [-f FORMAT]\n\t\
c65df279 639[-o OUTPUT] [FILE]", "\
5c3f75ec 640Options:\n\
641\n\
642-a, --armour Same as `-f pem'.\n\
643-f, --format=FORMAT Encode as FORMAT.\n\
c65df279 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\
5c3f75ec 646-o, --output=FILE Write output to FILE.\n\
cd6eca43 647-p, --progress Show progress on large files.\n\
946c3f72 648-C, --nocheck Don't check the public key.\n\
5c3f75ec 649" },
650 { "decrypt", decrypt,
cd6eca43 651 "decrypt [-abpqvC] [-f FORMAT] [-o OUTPUT] [FILE]", "\
5c3f75ec 652Options:\n\
653\n\
c65df279 654-a, --armour Same as `-f pem'.\n\
fa54fe1e 655-b, --buffer Buffer output until we're sure we have it all.\n\
c65df279 656-f, --format=FORMAT Decode as FORMAT.\n\
5c3f75ec 657-o, --output=FILE Write output to FILE.\n\
cd6eca43 658-p, --progress Show progress on large files.\n\
fa54fe1e 659-q, --quiet Produce fewer messages.\n\
660-v, --verbose Produce more verbose messages.\n\
946c3f72 661-C, --nocheck Don't check the private key.\n\
cd6eca43 662" },
5c3f75ec 663 { 0, 0, 0 }
664};
665
c65df279 666static int cmd_help(int argc, char **argv)
5c3f75ec 667{
c65df279 668 sc_help(cmdtab, stdout, argv + 1);
669 return (0);
5c3f75ec 670}
671
c65df279 672void version(FILE *fp)
5c3f75ec 673{
674 pquis(fp, "$, Catacomb version " VERSION "\n");
675}
676
677static void usage(FILE *fp)
678{
c65df279 679 pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n");
5c3f75ec 680}
681
c65df279 682void help_global(FILE *fp)
5c3f75ec 683{
c65df279 684 usage(fp);
685 fputs("\n\
ada55141 686Encrypt and decrypt files.\n\
c65df279 687\n\
688Global 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);
5c3f75ec 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 *
c65df279 705 * Use: Encrypts or decrypts files.
5c3f75ec 706 */
707
708int 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':
c65df279 736 sc_help(cmdtab, stdout, argv + optind);
5c3f75ec 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
c65df279 765 return (findcmd(cmdtab, argv[0])->cmd(argc, argv));
5c3f75ec 766
767#undef f_bogus
768}
769
770/*----- That's all, folks -------------------------------------------------*/