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