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