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