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