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