| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Command-line encryption tool |
| 4 | * |
| 5 | * (c) 2004 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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 | |
| 30 | #define _FILE_OFFSET_BITS 64 |
| 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/macros.h> |
| 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" |
| 48 | #include "ct.h" |
| 49 | #include "rand.h" |
| 50 | #include "noise.h" |
| 51 | #include "mprand.h" |
| 52 | #include "key.h" |
| 53 | #include "cc.h" |
| 54 | |
| 55 | #include "gaead.h" |
| 56 | #include "ectab.h" |
| 57 | #include "ptab.h" |
| 58 | |
| 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 |
| 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. |
| 77 | * |
| 78 | * If the message is signed, there comes a signature chunk. The signature is |
| 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. |
| 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 | { |
| 132 | const char *fn, *of = 0, *kn = "ccrypt", *skn = 0; |
| 133 | FILE *ofp = 0; |
| 134 | FILE *fp = 0; |
| 135 | const char *ef = "binary"; |
| 136 | fprogress ff; |
| 137 | const char *err; |
| 138 | int i; |
| 139 | int en; |
| 140 | size_t n, chsz; |
| 141 | dstr d = DSTR_INIT; |
| 142 | buf b; |
| 143 | size_t seq; |
| 144 | char bb[65536]; |
| 145 | unsigned f = 0; |
| 146 | key_file kf; |
| 147 | key *k; |
| 148 | key *sk = 0; |
| 149 | kem *km; |
| 150 | bulk *bc; |
| 151 | sig *s = 0; |
| 152 | const encops *eo; |
| 153 | enc *e; |
| 154 | |
| 155 | #define f_bogus 1u |
| 156 | #define f_nocheck 2u |
| 157 | #define f_progress 4u |
| 158 | |
| 159 | for (;;) { |
| 160 | static const struct option opt[] = { |
| 161 | { "key", OPTF_ARGREQ, 0, 'k' }, |
| 162 | { "sign-key", OPTF_ARGREQ, 0, 's' }, |
| 163 | { "armour", 0, 0, 'a' }, |
| 164 | { "armor", 0, 0, 'a' }, |
| 165 | { "format", OPTF_ARGREQ, 0, 'f' }, |
| 166 | { "output", OPTF_ARGREQ, 0, 'o' }, |
| 167 | { "progress", 0, 0, 'p' }, |
| 168 | { "nocheck", 0, 0, 'C' }, |
| 169 | { 0, 0, 0, 0 } |
| 170 | }; |
| 171 | i = mdwopt(argc, argv, "k:s:af:o:pC", opt, 0, 0, 0); |
| 172 | if (i < 0) break; |
| 173 | switch (i) { |
| 174 | case 'k': kn = optarg; break; |
| 175 | case 's': skn = optarg; break; |
| 176 | case 'a': ef = "pem"; break; |
| 177 | case 'f': ef = optarg; break; |
| 178 | case 'o': of = optarg; break; |
| 179 | case 'p': f |= f_progress; break; |
| 180 | case 'C': f |= f_nocheck; break; |
| 181 | default: f |= f_bogus; break; |
| 182 | } |
| 183 | } |
| 184 | if (argc - optind > 1 || (f & f_bogus)) |
| 185 | die(EXIT_FAILURE, "Usage: encrypt [-OPTIONS] [FILE]"); |
| 186 | |
| 187 | if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0)) |
| 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); |
| 191 | if (skn && (sk = key_bytag(&kf, skn)) == 0) |
| 192 | die(EXIT_FAILURE, "key `%s' not found", skn); |
| 193 | |
| 194 | if ((eo = getenc(ef)) == 0) |
| 195 | die(EXIT_FAILURE, "encoding `%s' not found", ef); |
| 196 | |
| 197 | fn = optind < argc ? argv[optind++] : "-"; |
| 198 | if (STRCMP(fn, ==, "-")) |
| 199 | fp = stdin; |
| 200 | else if ((fp = fopen(fn, "rb")) == 0) { |
| 201 | die(EXIT_FAILURE, "couldn't open file `%s': %s", |
| 202 | fn, strerror(errno)); |
| 203 | } |
| 204 | |
| 205 | if (!of || STRCMP(of, ==, "-")) |
| 206 | ofp = stdout; |
| 207 | else if ((ofp = fopen(of, eo->wmode)) == 0) { |
| 208 | die(EXIT_FAILURE, "couldn't open file `%s' for output: %s", |
| 209 | of, strerror(errno)); |
| 210 | } |
| 211 | |
| 212 | dstr_reset(&d); |
| 213 | key_fulltag(k, &d); |
| 214 | e = initenc(eo, ofp, "CATCRYPT ENCRYPTED MESSAGE"); |
| 215 | km = getkem(k, "cckem", 0, &bc); |
| 216 | if (!(f & f_nocheck) && (err = km->ops->check(km)) != 0) |
| 217 | moan("key %s fails check: %s", d.buf, err); |
| 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) |
| 223 | moan("key %s fails check: %s", d.buf, err); |
| 224 | } |
| 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); |
| 232 | if (sk) buf_putu32(&b, sk->id); |
| 233 | assert(BOK(&b)); |
| 234 | if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b)); |
| 235 | chunk_write(e, &b); |
| 236 | |
| 237 | /* --- Build the KEM chunk --- */ |
| 238 | |
| 239 | dstr_reset(&d); |
| 240 | if (setupkem(km, &d, bc)) die(EXIT_FAILURE, "failed to encapsulate key"); |
| 241 | buf_init(&b, d.buf, d.len); |
| 242 | BSTEP(&b, d.len); |
| 243 | if (s) GH_HASHBUF16(s->h, BBASE(&b), BLEN(&b)); |
| 244 | chunk_write(e, &b); |
| 245 | |
| 246 | /* --- Write the signature chunk --- */ |
| 247 | |
| 248 | if (s) { |
| 249 | GC_ENCRYPT(km->cx, 0, bb, 1024); |
| 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); |
| 257 | } |
| 258 | |
| 259 | /* --- Now do the main crypto --- */ |
| 260 | |
| 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 | |
| 268 | n = bc->ops->overhead(bc); |
| 269 | dstr_ensure(&d, sizeof(bb) + n); |
| 270 | seq = 0; |
| 271 | chsz = MASK16 - n; |
| 272 | do { |
| 273 | n = fread(bb, 1, chsz, fp); |
| 274 | if (f & f_progress) fprogress_update(&ff, n); |
| 275 | buf_init(&b, d.buf, d.sz); |
| 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++; |
| 281 | chunk_write(e, &b); |
| 282 | } while (n); |
| 283 | |
| 284 | /* --- All done --- */ |
| 285 | |
| 286 | if (f & f_progress) fprogress_done(&ff); |
| 287 | e->ops->encdone(e); |
| 288 | bc->ops->destroy(bc); |
| 289 | freeenc(e); |
| 290 | if (s) freesig(s); |
| 291 | freekem(km); |
| 292 | if (fp != stdin) fclose(fp); |
| 293 | if (of) fclose(ofp); |
| 294 | key_close(&kf); |
| 295 | dstr_destroy(&d); |
| 296 | return (0); |
| 297 | |
| 298 | #undef f_bogus |
| 299 | #undef f_nocheck |
| 300 | #undef f_progress |
| 301 | } |
| 302 | |
| 303 | /*---- Decryption ---------------------------------------------------------*/ |
| 304 | |
| 305 | static int decrypt(int argc, char *argv[]) |
| 306 | { |
| 307 | const char *fn, *of = 0; |
| 308 | FILE *ofp = 0, *rfp = 0; |
| 309 | FILE *fp = 0; |
| 310 | const char *ef = "binary"; |
| 311 | fprogress ff; |
| 312 | int i; |
| 313 | size_t n; |
| 314 | dstr d = DSTR_INIT; |
| 315 | char bb[65536]; |
| 316 | buf b; |
| 317 | key_file kf; |
| 318 | size_t seq; |
| 319 | uint32 id; |
| 320 | key *k; |
| 321 | key *sk = 0; |
| 322 | kem *km; |
| 323 | sig *s = 0; |
| 324 | bulk *bc; |
| 325 | unsigned f = 0; |
| 326 | const encops *eo; |
| 327 | const char *err; |
| 328 | int verb = 1; |
| 329 | enc *e; |
| 330 | |
| 331 | #define f_bogus 1u |
| 332 | #define f_buffer 2u |
| 333 | #define f_nocheck 4u |
| 334 | #define f_progress 8u |
| 335 | |
| 336 | for (;;) { |
| 337 | static const struct option opt[] = { |
| 338 | { "armour", 0, 0, 'a' }, |
| 339 | { "armor", 0, 0, 'a' }, |
| 340 | { "buffer", 0, 0, 'b' }, |
| 341 | { "verbose", 0, 0, 'v' }, |
| 342 | { "quiet", 0, 0, 'q' }, |
| 343 | { "nocheck", 0, 0, 'C' }, |
| 344 | { "format", OPTF_ARGREQ, 0, 'f' }, |
| 345 | { "output", OPTF_ARGREQ, 0, 'o' }, |
| 346 | { "progress", 0, 0, 'p' }, |
| 347 | { 0, 0, 0, 0 } |
| 348 | }; |
| 349 | i = mdwopt(argc, argv, "abf:o:pqvC", opt, 0, 0, 0); |
| 350 | if (i < 0) break; |
| 351 | switch (i) { |
| 352 | case 'a': ef = "pem"; break; |
| 353 | case 'b': f |= f_buffer; break; |
| 354 | case 'v': verb++; break; |
| 355 | case 'q': if (verb) verb--; break; |
| 356 | case 'C': f |= f_nocheck; break; |
| 357 | case 'f': ef = optarg; break; |
| 358 | case 'o': of = optarg; break; |
| 359 | case 'p': f |= f_progress; break; |
| 360 | default: f |= f_bogus; break; |
| 361 | } |
| 362 | } |
| 363 | if (argc - optind > 1 || (f & f_bogus)) |
| 364 | die(EXIT_FAILURE, "Usage: decrypt [-OPTIONS] [FILE]"); |
| 365 | |
| 366 | if ((eo = getenc(ef)) == 0) |
| 367 | die(EXIT_FAILURE, "encoding `%s' not found", ef); |
| 368 | |
| 369 | fn = optind < argc ? argv[optind++] : "-"; |
| 370 | if (STRCMP(fn, ==, "-")) |
| 371 | fp = stdin; |
| 372 | else if ((fp = fopen(fn, eo->rmode)) == 0) { |
| 373 | die(EXIT_FAILURE, "couldn't open file `%s': %s", |
| 374 | fn, strerror(errno)); |
| 375 | } |
| 376 | |
| 377 | if (key_open(&kf, keyring, KOPEN_READ, key_moan, 0)) |
| 378 | die(EXIT_FAILURE, "can't open keyring `%s'", keyring); |
| 379 | |
| 380 | e = initdec(eo, fp, checkbdry, "CATCRYPT ENCRYPTED MESSAGE"); |
| 381 | |
| 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 | |
| 389 | /* --- Read the header chunk --- */ |
| 390 | |
| 391 | chunk_read(e, &d, &b); |
| 392 | if (f & f_progress) |
| 393 | fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw); |
| 394 | if (buf_getu32(&b, &id)) { |
| 395 | if (f & f_progress) fprogress_done(&ff); |
| 396 | if (verb) printf("FAIL malformed header: missing keyid\n"); |
| 397 | exit(EXIT_FAILURE); |
| 398 | } |
| 399 | if ((k = key_byid(&kf, id)) == 0) { |
| 400 | if (f & f_progress) fprogress_done(&ff); |
| 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)) { |
| 406 | if (f & f_progress) fprogress_done(&ff); |
| 407 | if (verb) printf("FAIL malformed header: missing signature keyid\n"); |
| 408 | exit(EXIT_FAILURE); |
| 409 | } |
| 410 | if ((sk = key_byid(&kf, id)) == 0) { |
| 411 | if (f & f_progress) fprogress_done(&ff); |
| 412 | if (verb) printf("FAIL key id %08lx not found\n", (unsigned long)id); |
| 413 | exit(EXIT_FAILURE); |
| 414 | } |
| 415 | } |
| 416 | if (BLEFT(&b)) { |
| 417 | if (f & f_progress) fprogress_done(&ff); |
| 418 | if (verb) printf("FAIL malformed header: junk at end\n"); |
| 419 | exit(EXIT_FAILURE); |
| 420 | } |
| 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 | } |
| 430 | |
| 431 | /* --- Find the key --- */ |
| 432 | |
| 433 | km = getkem(k, "cckem", 1, &bc); |
| 434 | |
| 435 | /* --- Read the KEM chunk --- */ |
| 436 | |
| 437 | chunk_read(e, &d, &b); |
| 438 | if (f & f_progress) |
| 439 | fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw); |
| 440 | if (setupkem(km, &d, bc)) { |
| 441 | if (f & f_progress) fprogress_done(&ff); |
| 442 | if (verb) printf("FAIL failed to decapsulate key\n"); |
| 443 | exit(EXIT_FAILURE); |
| 444 | } |
| 445 | if (s) GH_HASHBUF16(s->h, d.buf, d.len); |
| 446 | |
| 447 | /* --- Verify the signature, if there is one --- */ |
| 448 | |
| 449 | if (sk) { |
| 450 | dstr_reset(&d); |
| 451 | dstr_ensure(&d, 1024); |
| 452 | GC_ENCRYPT(km->cx, 0, d.buf, 1024); |
| 453 | GH_HASH(s->h, d.buf, 1024); |
| 454 | chunk_read(e, &d, &b); |
| 455 | if (f & f_progress) |
| 456 | fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw); |
| 457 | if (s->ops->doit(s, &d)) { |
| 458 | if (f & f_progress) fprogress_done(&ff); |
| 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); |
| 465 | if (f & f_progress) fprogress_clear(&ff); |
| 466 | printf("INFO good-signature %s\n", d.buf); |
| 467 | } |
| 468 | freesig(s); |
| 469 | } else if (verb) { |
| 470 | if (f & f_progress) fprogress_clear(&ff); |
| 471 | printf("INFO no-signature\n"); |
| 472 | } |
| 473 | |
| 474 | /* --- Now decrypt the main body --- */ |
| 475 | |
| 476 | if (!of || STRCMP(of, ==, "-")) { |
| 477 | ofp = stdout; |
| 478 | f |= f_buffer; |
| 479 | } |
| 480 | if (!(f & f_buffer)) { |
| 481 | if ((ofp = fopen(of, "wb")) == 0) { |
| 482 | if (f & f_progress) fprogress_done(&ff); |
| 483 | die(EXIT_FAILURE, "couldn't open file `%s' for output: %s", |
| 484 | of, strerror(errno)); |
| 485 | } |
| 486 | rfp = ofp; |
| 487 | } else if ((rfp = tmpfile()) == 0) { |
| 488 | if (f & f_progress) fprogress_done(&ff); |
| 489 | die(EXIT_FAILURE, "couldn't create temporary file: %s", strerror(errno)); |
| 490 | } |
| 491 | |
| 492 | seq = 0; |
| 493 | dstr_ensure(&d, bc->ops->overhead(bc)); |
| 494 | dstr_ensure(&d, 4); |
| 495 | for (;;) { |
| 496 | chunk_read(e, &d, &b); |
| 497 | if (f & f_progress) |
| 498 | fprogress_update(&ff, BLEFT(&b)*e->ops->ncook/e->ops->nraw); |
| 499 | buf_init(&b, bb, sizeof(bb)); |
| 500 | if ((err = bc->ops->doit(bc, seq, &b, d.buf, d.len)) != 0) { |
| 501 | if (f & f_progress) fprogress_done(&ff); |
| 502 | if (verb) printf("FAIL bad ciphertext chunk: %s\n", err); |
| 503 | exit(EXIT_FAILURE); |
| 504 | } |
| 505 | seq++; |
| 506 | if (!BLEN(&b)) break; |
| 507 | if (fwrite(BBASE(&b), 1, BLEN(&b), rfp) != BLEN(&b)) { |
| 508 | if (f & f_progress) fprogress_done(&ff); |
| 509 | if (verb) printf("FAIL error writing output: %s\n", strerror(errno)); |
| 510 | exit(EXIT_FAILURE); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (f & f_progress) fprogress_done(&ff); |
| 515 | if (fflush(rfp) || ferror(rfp)) { |
| 516 | if (verb) printf("FAIL error writing output: %s\n", strerror(errno)); |
| 517 | exit(EXIT_FAILURE); |
| 518 | } |
| 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); |
| 525 | if (f & f_progress) fprogress_init(&ff, "copying buffer", rfp); |
| 526 | dstr_reset(&d); |
| 527 | dstr_ensure(&d, 65536); |
| 528 | if (verb && ofp == stdout) printf("DATA\n"); |
| 529 | for (;;) { |
| 530 | n = fread(d.buf, 1, d.sz, rfp); |
| 531 | if (!n) break; |
| 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); |
| 535 | die(EXIT_FAILURE, "error writing output: %s", strerror(errno)); |
| 536 | } |
| 537 | } |
| 538 | if (f & f_progress) fprogress_done(&ff); |
| 539 | if (ferror(rfp) || fclose(rfp)) |
| 540 | die(EXIT_FAILURE, "error unbuffering output: %s", strerror(errno)); |
| 541 | } |
| 542 | |
| 543 | e->ops->decdone(e); |
| 544 | if (verb && ofp != stdout) |
| 545 | printf("OK decrypted successfully\n"); |
| 546 | if (ofp && (fflush(ofp) || ferror(ofp) || fclose(ofp))) |
| 547 | die(EXIT_FAILURE, "error writing output: %s", strerror(errno)); |
| 548 | bc->ops->destroy(bc); |
| 549 | freeenc(e); |
| 550 | freekem(km); |
| 551 | if (fp != stdin) fclose(fp); |
| 552 | key_close(&kf); |
| 553 | dstr_destroy(&d); |
| 554 | return (0); |
| 555 | |
| 556 | #undef f_bogus |
| 557 | #undef f_buffer |
| 558 | #undef f_nocheck |
| 559 | #undef f_progress |
| 560 | } |
| 561 | |
| 562 | /*----- Main code ---------------------------------------------------------*/ |
| 563 | |
| 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) \ |
| 569 | LI("Bulk crypto transforms", bulk, \ |
| 570 | bulktab[i].name, bulktab[i].name) \ |
| 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) \ |
| 577 | LI("Authenticated encryption schemes", aead, \ |
| 578 | gaeadtab[i], gaeadtab[i]->name) \ |
| 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 **); |
| 592 | |
| 593 | static cmd cmdtab[] = { |
| 594 | { "help", cmd_help, "help [COMMAND...]" }, |
| 595 | { "show", cmd_show, "show [ITEM...]" }, |
| 596 | CMD_ENCODE, |
| 597 | CMD_DECODE, |
| 598 | { "encrypt", encrypt, |
| 599 | "encrypt [-apC] [-k TAG] [-s TAG] [-f FORMAT]\n\t\ |
| 600 | [-o OUTPUT] [FILE]", "\ |
| 601 | Options:\n\ |
| 602 | \n\ |
| 603 | -a, --armour Same as `-f pem'.\n\ |
| 604 | -f, --format=FORMAT Encode as FORMAT.\n\ |
| 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\ |
| 607 | -o, --output=FILE Write output to FILE.\n\ |
| 608 | -p, --progress Show progress on large files.\n\ |
| 609 | -C, --nocheck Don't check the public key.\n\ |
| 610 | " }, |
| 611 | { "decrypt", decrypt, |
| 612 | "decrypt [-abpqvC] [-f FORMAT] [-o OUTPUT] [FILE]", "\ |
| 613 | Options:\n\ |
| 614 | \n\ |
| 615 | -a, --armour Same as `-f pem'.\n\ |
| 616 | -b, --buffer Buffer output until we're sure we have it all.\n\ |
| 617 | -f, --format=FORMAT Decode as FORMAT.\n\ |
| 618 | -o, --output=FILE Write output to FILE.\n\ |
| 619 | -p, --progress Show progress on large files.\n\ |
| 620 | -q, --quiet Produce fewer messages.\n\ |
| 621 | -v, --verbose Produce more verbose messages.\n\ |
| 622 | -C, --nocheck Don't check the private key.\n\ |
| 623 | " }, |
| 624 | { 0, 0, 0 } |
| 625 | }; |
| 626 | |
| 627 | static int cmd_help(int argc, char **argv) |
| 628 | { |
| 629 | sc_help(cmdtab, stdout, argv + 1); |
| 630 | return (0); |
| 631 | } |
| 632 | |
| 633 | void version(FILE *fp) |
| 634 | { |
| 635 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
| 636 | } |
| 637 | |
| 638 | static void usage(FILE *fp) |
| 639 | { |
| 640 | pquis(fp, "Usage: $ [-k KEYRING] COMMAND [ARGS]\n"); |
| 641 | } |
| 642 | |
| 643 | void help_global(FILE *fp) |
| 644 | { |
| 645 | usage(fp); |
| 646 | fputs("\n\ |
| 647 | Encrypt and decrypt files.\n\ |
| 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); |
| 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 | * |
| 666 | * Use: Encrypts or decrypts files. |
| 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': |
| 697 | sc_help(cmdtab, stdout, argv + optind); |
| 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 | |
| 726 | return (findcmd(cmdtab, argv[0])->cmd(argc, argv)); |
| 727 | |
| 728 | #undef f_bogus |
| 729 | } |
| 730 | |
| 731 | /*----- That's all, folks -------------------------------------------------*/ |