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; |
5c3f75ec | 136 | size_t n; |
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; | |
264 | for (;;) { | |
265 | h = GM_INIT(m); | |
dfd1608a | 266 | GH_HASHU32(h, seq); |
5c3f75ec | 267 | seq++; |
268 | if (GC_CLASS(c)->blksz) { | |
269 | GC_ENCRYPT(cx, 0, bb, GC_CLASS(c)->blksz); | |
270 | GC_SETIV(c, bb); | |
271 | } | |
272 | n = fread(bb, 1, sizeof(bb), fp); | |
273 | if (!n) break; | |
274 | buf_init(&b, d.buf, d.sz); | |
275 | tag = buf_get(&b, GM_CLASS(m)->hashsz); | |
276 | ct = buf_get(&b, n); | |
277 | assert(tag); assert(ct); | |
278 | GC_ENCRYPT(c, bb, ct, n); | |
279 | GH_HASH(h, ct, n); | |
280 | GH_DONE(h, tag); | |
281 | GH_DESTROY(h); | |
282 | chunk_write(e, &b); | |
283 | } | |
284 | ||
285 | /* --- Final terminator packet --- */ | |
286 | ||
287 | buf_init(&b, d.buf, d.sz); | |
288 | tag = buf_get(&b, GM_CLASS(m)->hashsz); | |
289 | assert(tag); | |
290 | GH_DONE(h, tag); | |
291 | GH_DESTROY(h); | |
292 | chunk_write(e, &b); | |
293 | ||
294 | /* --- All done --- */ | |
295 | ||
296 | e->ops->encdone(e); | |
297 | GM_DESTROY(m); | |
298 | GC_DESTROY(c); | |
299 | GC_DESTROY(cx); | |
300 | freeenc(e); | |
c65df279 | 301 | if (s) freesig(s); |
5c3f75ec | 302 | freekem(km); |
ef13e9a4 | 303 | if (fp != stdin) fclose(fp); |
5c3f75ec | 304 | if (of) fclose(ofp); |
305 | key_close(&kf); | |
306 | dstr_destroy(&d); | |
307 | return (0); | |
308 | ||
309 | #undef f_bogus | |
946c3f72 | 310 | #undef f_nocheck |
5c3f75ec | 311 | } |
312 | ||
313 | /*---- Decryption ---------------------------------------------------------*/ | |
314 | ||
315 | static int decrypt(int argc, char *argv[]) | |
316 | { | |
317 | const char *of = 0; | |
fa54fe1e | 318 | FILE *ofp = 0, *rfp = 0; |
5c3f75ec | 319 | FILE *fp = 0; |
320 | const char *ef = "binary"; | |
321 | int i; | |
fa54fe1e | 322 | size_t n; |
5c3f75ec | 323 | dstr d = DSTR_INIT; |
324 | buf b; | |
325 | key_file kf; | |
326 | size_t seq; | |
327 | uint32 id; | |
328 | key *k; | |
c65df279 | 329 | key *sk = 0; |
5c3f75ec | 330 | kem *km; |
c65df279 | 331 | sig *s = 0; |
5c3f75ec | 332 | gcipher *cx; |
333 | gcipher *c; | |
334 | ghash *h; | |
335 | gmac *m; | |
336 | octet *tag; | |
337 | unsigned f = 0; | |
338 | const encops *eo; | |
c65df279 | 339 | const char *err; |
340 | int verb = 1; | |
5c3f75ec | 341 | enc *e; |
342 | ||
343 | #define f_bogus 1u | |
fa54fe1e | 344 | #define f_buffer 2u |
946c3f72 | 345 | #define f_nocheck 4u |
5c3f75ec | 346 | |
347 | for (;;) { | |
348 | static const struct option opt[] = { | |
349 | { "armour", 0, 0, 'a' }, | |
350 | { "armor", 0, 0, 'a' }, | |
fa54fe1e | 351 | { "buffer", 0, 0, 'b' }, |
c65df279 | 352 | { "verbose", 0, 0, 'v' }, |
353 | { "quiet", 0, 0, 'q' }, | |
946c3f72 | 354 | { "nocheck", 0, 0, 'C' }, |
45c0fd36 MW |
355 | { "format", OPTF_ARGREQ, 0, 'f' }, |
356 | { "output", OPTF_ARGREQ, 0, 'o' }, | |
5c3f75ec | 357 | { 0, 0, 0, 0 } |
358 | }; | |
946c3f72 | 359 | i = mdwopt(argc, argv, "abf:o:qvC", opt, 0, 0, 0); |
5c3f75ec | 360 | if (i < 0) break; |
361 | switch (i) { | |
362 | case 'a': ef = "pem"; break; | |
fa54fe1e | 363 | case 'b': f |= f_buffer; break; |
c65df279 | 364 | case 'v': verb++; break; |
365 | case 'q': if (verb) verb--; break; | |
946c3f72 | 366 | case 'C': f |= f_nocheck; break; |
5c3f75ec | 367 | case 'f': ef = optarg; break; |
368 | case 'o': of = optarg; break; | |
369 | default: f |= f_bogus; break; | |
370 | } | |
371 | } | |
372 | if (argc - optind > 1 || (f & f_bogus)) | |
c65df279 | 373 | die(EXIT_FAILURE, "Usage: decrypt [-OPTIONS] [FILE]"); |
5c3f75ec | 374 | |
375 | if ((eo = getenc(ef)) == 0) | |
376 | die(EXIT_FAILURE, "encoding `%s' not found", ef); | |
377 | ||
378 | if (optind == argc) | |
379 | fp = stdin; | |
380 | else if (strcmp(argv[optind], "-") == 0) { | |
381 | fp = stdin; | |
382 | optind++; | |
383 | } else if ((fp = fopen(argv[optind], eo->rmode)) == 0) { | |
384 | die(EXIT_FAILURE, "couldn't open file `%s': %s", | |
385 | argv[optind], strerror(errno)); | |
386 | } else | |
387 | optind++; | |
388 | ||
213e565f | 389 | if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0)) |
5c3f75ec | 390 | die(EXIT_FAILURE, "can't open keyring `%s'", keyring); |
391 | ||
fa54fe1e | 392 | e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE"); |
5c3f75ec | 393 | |
394 | /* --- Read the header chunk --- */ | |
395 | ||
396 | chunk_read(e, &d, &b); | |
c65df279 | 397 | if (buf_getu32(&b, &id)) { |
398 | if (verb) printf("FAIL malformed header: missing keyid\n"); | |
399 | exit(EXIT_FAILURE); | |
400 | } | |
401 | if ((k = key_byid(&kf, id)) == 0) { | |
402 | if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id); | |
403 | exit(EXIT_FAILURE); | |
404 | } | |
405 | if (BLEFT(&b)) { | |
406 | if (buf_getu32(&b, &id)) { | |
407 | if (verb) printf("FAIL malformed header: missing signature keyid\n"); | |
408 | exit(EXIT_FAILURE); | |
409 | } | |
410 | if ((sk = key_byid(&kf, id)) == 0) { | |
411 | if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id); | |
412 | exit(EXIT_FAILURE); | |
413 | } | |
414 | } | |
415 | if (BLEFT(&b)) { | |
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 | ||
c65df279 | 431 | km = getkem(k, "cckem", 1); |
5c3f75ec | 432 | |
433 | /* --- Read the KEM chunk --- */ | |
434 | ||
435 | chunk_read(e, &d, &b); | |
c65df279 | 436 | if (setupkem(km, &d, &cx, &c, &m)) { |
437 | if (verb) printf("FAIL failed to decapsulate key\n"); | |
438 | exit(EXIT_FAILURE); | |
439 | } | |
f9e51332 | 440 | if (s) GH_HASHBUF16(s->h, d.buf, d.len); |
c65df279 | 441 | |
442 | /* --- Verify the signature, if there is one --- */ | |
443 | ||
444 | if (sk) { | |
c65df279 | 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); | |
fa54fe1e | 457 | printf("INFO good-signature %s\n", d.buf); |
c65df279 | 458 | } |
ef13e9a4 | 459 | freesig(s); |
c65df279 | 460 | } else if (verb) |
fa54fe1e | 461 | printf("INFO no-signature\n"); |
5c3f75ec | 462 | |
463 | /* --- Now decrypt the main body --- */ | |
464 | ||
c65df279 | 465 | if (!of || strcmp(of, "-") == 0) { |
5c3f75ec | 466 | ofp = stdout; |
fa54fe1e | 467 | f |= f_buffer; |
5c3f75ec | 468 | } |
fa54fe1e | 469 | if (!(f & f_buffer)) { |
470 | if ((ofp = fopen(of, "wb")) == 0) { | |
471 | die(EXIT_FAILURE, "couldn't open file `%s' for output: %s", | |
472 | ofp, strerror(errno)); | |
473 | } | |
474 | rfp = ofp; | |
475 | } else if ((rfp = tmpfile()) == 0) | |
476 | die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno)); | |
5c3f75ec | 477 | |
478 | seq = 0; | |
479 | dstr_ensure(&d, GC_CLASS(c)->blksz); | |
480 | dstr_ensure(&d, 4); | |
481 | for (;;) { | |
482 | if (GC_CLASS(c)->blksz) { | |
483 | GC_ENCRYPT(cx, 0, d.buf, GC_CLASS(c)->blksz); | |
484 | GC_SETIV(c, d.buf); | |
485 | } | |
486 | h = GM_INIT(m); | |
dfd1608a | 487 | GH_HASHU32(h, seq); |
5c3f75ec | 488 | seq++; |
489 | chunk_read(e, &d, &b); | |
c65df279 | 490 | if ((tag = buf_get(&b, GM_CLASS(m)->hashsz)) == 0) { |
fa54fe1e | 491 | if (verb) printf("FAIL bad ciphertext chunk: no tag\n"); |
c65df279 | 492 | exit(EXIT_FAILURE); |
493 | } | |
5c3f75ec | 494 | GH_HASH(h, BCUR(&b), BLEFT(&b)); |
c65df279 | 495 | if (memcmp(tag, GH_DONE(h, 0), GM_CLASS(m)->hashsz) != 0) { |
496 | if (verb) | |
fa54fe1e | 497 | printf("FAIL bad ciphertext chunk: authentication failure\n"); |
c65df279 | 498 | exit(EXIT_FAILURE); |
499 | } | |
ef13e9a4 | 500 | GH_DESTROY(h); |
5c3f75ec | 501 | if (!BLEFT(&b)) |
502 | break; | |
503 | GC_DECRYPT(c, BCUR(&b), BCUR(&b), BLEFT(&b)); | |
fa54fe1e | 504 | if (fwrite(BCUR(&b), 1, BLEFT(&b), rfp) != BLEFT(&b)) { |
505 | if (verb) printf("FAIL error writing output: %s\n", strerror(errno)); | |
c65df279 | 506 | exit(EXIT_FAILURE); |
507 | } | |
508 | } | |
509 | ||
fa54fe1e | 510 | if (fflush(rfp) || ferror(rfp)) { |
511 | if (verb) printf("FAIL error writing output: %s\n", strerror(errno)); | |
c65df279 | 512 | exit(EXIT_FAILURE); |
5c3f75ec | 513 | } |
fa54fe1e | 514 | if (f & f_buffer) { |
515 | if (!ofp && (ofp = fopen(of, "wb")) == 0) { | |
516 | die(EXIT_FAILURE, "couldn't open file `%s' for output: %s", | |
517 | of, strerror(errno)); | |
518 | } | |
519 | rewind(rfp); | |
520 | dstr_reset(&d); | |
521 | dstr_ensure(&d, 65536); | |
64459c98 | 522 | if (verb && ofp == stdout) printf("DATA\n"); |
fa54fe1e | 523 | for (;;) { |
524 | n = fread(d.buf, 1, d.sz, rfp); | |
525 | if (!n) break; | |
526 | if (fwrite(d.buf, 1, n, ofp) < n) | |
527 | die(EXIT_FAILURE, "error writing output: %s", strerror(errno)); | |
528 | } | |
529 | if (ferror(rfp) || fclose(rfp)) | |
530 | die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno)); | |
531 | } | |
532 | if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp))) | |
45c0fd36 | 533 | die(EXIT_FAILURE, "error writing output: %s", strerror(errno)); |
5c3f75ec | 534 | |
5c3f75ec | 535 | e->ops->decdone(e); |
c65df279 | 536 | if (verb && ofp != stdout) |
537 | printf("OK decrypted successfully\n"); | |
5c3f75ec | 538 | freeenc(e); |
539 | GC_DESTROY(c); | |
540 | GC_DESTROY(cx); | |
541 | GM_DESTROY(m); | |
542 | freekem(km); | |
ef13e9a4 | 543 | if (fp != stdin) fclose(fp); |
5c3f75ec | 544 | if (of) fclose(ofp); |
545 | key_close(&kf); | |
546 | dstr_destroy(&d); | |
547 | return (0); | |
548 | ||
549 | #undef f_bogus | |
fa54fe1e | 550 | #undef f_buffer |
946c3f72 | 551 | #undef f_nocheck |
5c3f75ec | 552 | } |
553 | ||
554 | /*----- Main code ---------------------------------------------------------*/ | |
555 | ||
c65df279 | 556 | #define LISTS(LI) \ |
557 | LI("Lists", list, \ | |
558 | listtab[i].name, listtab[i].name) \ | |
559 | LI("Key-encapsulation mechanisms", kem, \ | |
560 | kemtab[i].name, kemtab[i].name) \ | |
561 | LI("Signature schemes", sig, \ | |
562 | sigtab[i].name, sigtab[i].name) \ | |
563 | LI("Encodings", enc, \ | |
564 | enctab[i].name, enctab[i].name) \ | |
565 | LI("Symmetric encryption algorithms", cipher, \ | |
566 | gciphertab[i], gciphertab[i]->name) \ | |
567 | LI("Hash functions", hash, \ | |
568 | ghashtab[i], ghashtab[i]->name) \ | |
569 | LI("Message authentication codes", mac, \ | |
570 | gmactab[i], gmactab[i]->name) | |
571 | ||
572 | MAKELISTTAB(listtab, LISTS) | |
573 | ||
574 | int cmd_show(int argc, char *argv[]) | |
575 | { | |
576 | return (displaylists(listtab, argv + 1)); | |
577 | } | |
578 | ||
579 | static int cmd_help(int, char **); | |
5c3f75ec | 580 | |
581 | static cmd cmdtab[] = { | |
c65df279 | 582 | { "help", cmd_help, "help [COMMAND...]" }, |
583 | { "show", cmd_show, "show [ITEM...]" }, | |
fa54fe1e | 584 | CMD_ENCODE, |
585 | CMD_DECODE, | |
5c3f75ec | 586 | { "encrypt", encrypt, |
946c3f72 | 587 | "encrypt [-aC] [-k TAG] [-s TAG] [-f FORMAT]\n\t\ |
c65df279 | 588 | [-o OUTPUT] [FILE]", "\ |
5c3f75ec | 589 | Options:\n\ |
590 | \n\ | |
591 | -a, --armour Same as `-f pem'.\n\ | |
592 | -f, --format=FORMAT Encode as FORMAT.\n\ | |
c65df279 | 593 | -k, --key=TAG Use public encryption key named by TAG.\n\ |
594 | -s, --sign-key=TAG Use private signature key named by TAG.\n\ | |
5c3f75ec | 595 | -o, --output=FILE Write output to FILE.\n\ |
946c3f72 | 596 | -C, --nocheck Don't check the public key.\n\ |
5c3f75ec | 597 | " }, |
598 | { "decrypt", decrypt, | |
946c3f72 | 599 | "decrypt [-abqvC] [-f FORMAT] [-o OUTPUT] [FILE]", "\ |
5c3f75ec | 600 | Options:\n\ |
601 | \n\ | |
c65df279 | 602 | -a, --armour Same as `-f pem'.\n\ |
fa54fe1e | 603 | -b, --buffer Buffer output until we're sure we have it all.\n\ |
c65df279 | 604 | -f, --format=FORMAT Decode as FORMAT.\n\ |
5c3f75ec | 605 | -o, --output=FILE Write output to FILE.\n\ |
fa54fe1e | 606 | -q, --quiet Produce fewer messages.\n\ |
607 | -v, --verbose Produce more verbose messages.\n\ | |
946c3f72 | 608 | -C, --nocheck Don't check the private key.\n\ |
fa54fe1e | 609 | " }, /* ' emacs is confused */ |
5c3f75ec | 610 | { 0, 0, 0 } |
611 | }; | |
612 | ||
c65df279 | 613 | static int cmd_help(int argc, char **argv) |
5c3f75ec | 614 | { |
c65df279 | 615 | sc_help(cmdtab, stdout, argv + 1); |
616 | return (0); | |
5c3f75ec | 617 | } |
618 | ||
c65df279 | 619 | void version(FILE *fp) |
5c3f75ec | 620 | { |
621 | pquis(fp, "$, Catacomb version " VERSION "\n"); | |
622 | } | |
623 | ||
624 | static void usage(FILE *fp) | |
625 | { | |
c65df279 | 626 | pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n"); |
5c3f75ec | 627 | } |
628 | ||
c65df279 | 629 | void help_global(FILE *fp) |
5c3f75ec | 630 | { |
c65df279 | 631 | usage(fp); |
632 | fputs("\n\ | |
ada55141 | 633 | Encrypt and decrypt files.\n\ |
c65df279 | 634 | \n\ |
635 | Global command-line options:\n\ | |
636 | \n\ | |
637 | -h, --help [COMMAND...] Show this help message, or help for COMMANDs.\n\ | |
638 | -v, --version Show program version number.\n\ | |
639 | -u, --usage Show a terse usage message.\n\ | |
640 | \n\ | |
641 | -k, --keyring=FILE Read keys from FILE.\n", | |
642 | fp); | |
5c3f75ec | 643 | } |
644 | ||
645 | /* --- @main@ --- * | |
646 | * | |
647 | * Arguments: @int argc@ = number of command line arguments | |
648 | * @char *argv[]@ = vector of command line arguments | |
649 | * | |
650 | * Returns: Zero if successful, nonzero otherwise. | |
651 | * | |
c65df279 | 652 | * Use: Encrypts or decrypts files. |
5c3f75ec | 653 | */ |
654 | ||
655 | int main(int argc, char *argv[]) | |
656 | { | |
657 | unsigned f = 0; | |
658 | ||
659 | #define f_bogus 1u | |
660 | ||
661 | /* --- Initialize the library --- */ | |
662 | ||
663 | ego(argv[0]); | |
664 | sub_init(); | |
665 | rand_noisesrc(RAND_GLOBAL, &noise_source); | |
666 | rand_seed(RAND_GLOBAL, 160); | |
667 | ||
668 | /* --- Parse options --- */ | |
669 | ||
670 | for (;;) { | |
671 | static struct option opts[] = { | |
672 | { "help", 0, 0, 'h' }, | |
673 | { "version", 0, 0, 'v' }, | |
674 | { "usage", 0, 0, 'u' }, | |
675 | { "keyring", OPTF_ARGREQ, 0, 'k' }, | |
676 | { 0, 0, 0, 0 } | |
677 | }; | |
678 | int i = mdwopt(argc, argv, "+hvu k:", opts, 0, 0, 0); | |
679 | if (i < 0) | |
680 | break; | |
681 | switch (i) { | |
682 | case 'h': | |
c65df279 | 683 | sc_help(cmdtab, stdout, argv + optind); |
5c3f75ec | 684 | exit(0); |
685 | break; | |
686 | case 'v': | |
687 | version(stdout); | |
688 | exit(0); | |
689 | break; | |
690 | case 'u': | |
691 | usage(stdout); | |
692 | exit(0); | |
693 | case 'k': | |
694 | keyring = optarg; | |
695 | break; | |
696 | default: | |
697 | f |= f_bogus; | |
698 | break; | |
699 | } | |
700 | } | |
701 | ||
702 | argc -= optind; | |
703 | argv += optind; | |
704 | optind = 0; | |
705 | if (f & f_bogus || argc < 1) { | |
706 | usage(stderr); | |
707 | exit(EXIT_FAILURE); | |
708 | } | |
709 | ||
710 | /* --- Dispatch to the correct subcommand handler --- */ | |
711 | ||
c65df279 | 712 | return (findcmd(cmdtab, argv[0])->cmd(argc, argv)); |
5c3f75ec | 713 | |
714 | #undef f_bogus | |
715 | } | |
716 | ||
717 | /*----- That's all, folks -------------------------------------------------*/ |