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