1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
|
/* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar
*
* This file is part of Owl.
*
* Owl is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Owl is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Owl. If not, see <http://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------
*
* As of Owl version 2.1.12 there are patches contributed by
* developers of the branched BarnOwl project, Copyright (c)
* 2006-2009 The BarnOwl Developers. All rights reserved.
*/
/* The functions here with "owl" in their name were written for the
* Owl project. Other code is from Philip Lisiecki's
* (lisiecki@mit.edu) zcrypt program.
*/
/* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted *
* datastream. Reads the string to make the key from from the first *
* parameter. Encrypts or decrypts according to -d or -e flag. (-e is *
* default.) Will invoke zwrite if the -c option is provided for *
* encryption. If a zephyr class is specified & the keyfile name omitted *
* the ~/.crypt-table will be checked for "crypt-classname" and then *
* "crypt-default" for the keyfile name. */
static const char fileIdent[] = "$Id: zcrypt.c,v 1.13 2009/03/29 12:38:23 kretch Exp $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "owl.h"
#ifdef OWL_ENABLE_ZCRYPT
#define BASE_CODE 70
#define LAST_CODE (BASE_CODE + 15)
#define OUTPUT_BLOCK_SIZE 16
#include <unistd.h>
#include <sys/types.h>
#include <des.h>
#define MAX_KEY 128
#ifndef TRUE
#define TRUE -1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define ZWRITE_OPT_NOAUTH (1<<0)
#define ZWRITE_OPT_SIGNATURE (1<<1)
#define ZWRITE_OPT_IGNOREVARS (1<<2)
#define ZWRITE_OPT_VERBOSE (1<<3)
#define ZWRITE_OPT_QUIET (1<<4)
#define ZCRYPT_OPT_MESSAGE (1<<5)
#define ZCRYPT_OPT_IGNOREDOT (1<<6)
typedef struct
{
int flags;
char *signature;
char *message;
} ZWRITEOPTIONS;
char *GetZephyrVarKeyFile(char *whoami, char *class, char *instance);
char *BuildArgString(char **argv, int start, int end);
static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile);
int do_decrypt(char *keystring);
#ifndef HAVE_DES_ECB_ENCRYPT_PROTO
int des_ecb_encrypt(char [], char [], des_key_schedule, int);
#endif
#define M_NONE 0
#define M_ZEPHYR_ENCRYPT 1
#define M_DECRYPT 2
#define M_ENCRYPT 3
#define M_RANDOMIZE 4
#define M_SETKEY 5
int zcrypt(int argc, char *argv[]) {
char *fname = NULL;
int error = FALSE;
int zephyr = FALSE;
char *class = NULL, *instance = NULL;
int mode = M_NONE;
extern int optind, opterr;
extern char *optarg;
char c;
int messageflag = FALSE;
ZWRITEOPTIONS zoptions;
zoptions.flags = 0;
while ((c = getopt(argc, argv, "ZDERSF:c:i:advqtluons:f:m")) != (char)EOF) {
switch(c) {
case 'Z':
/* Zephyr encrypt */
mode = M_ZEPHYR_ENCRYPT;
break;
case 'D':
/* Decrypt */
mode = M_DECRYPT;
break;
case 'E':
/* Encrypt */
mode = M_ENCRYPT;
break;
case 'R':
/* Randomize the keyfile */
mode = M_RANDOMIZE;
break;
case 'S':
/* Set a new key value from stdin */
mode = M_SETKEY;
break;
case 'F':
/* Specify the keyfile explicitly */
if (fname != NULL) error = TRUE;
fname = optarg;
break;
case 'c':
/* Zwrite/zcrypt: class name */
if (class != NULL) error = TRUE;
class = optarg;
break;
case 'i':
/* Zwrite/zcrypt: instance name */
if (instance != NULL) error = TRUE;
instance = optarg;
break;
case 'a':
/* Zwrite: authenticate (default) */
zoptions.flags &= ~ZWRITE_OPT_NOAUTH;
break;
case 'd':
/* Zwrite: do not authenticate */
zoptions.flags |= ZWRITE_OPT_NOAUTH;
break;
case 'v':
/* Zwrite: verbose */
zoptions.flags |= ZWRITE_OPT_VERBOSE;
break;
case 'q':
/* Zwrite: quiet */
zoptions.flags |= ZWRITE_OPT_QUIET;
break;
case 't':
/* Zwrite: no expand tabs (ignored) */
break;
case 'l':
/* Zwrite: ignore '.' on a line by itself (ignored) */
zoptions.flags |= ZCRYPT_OPT_IGNOREDOT;
break;
case 'u':
/* Zwrite: urgent message */
instance = "URGENT";
break;
case 'o':
/* Zwrite: ignore zephyr variables zwrite-class, zwrite-inst, */
/* zwrite-opcode */
zoptions.flags |= ZWRITE_OPT_IGNOREVARS;
break;
case 'n':
/* Zwrite: prevent PING message (always used) */
break;
case 's':
/* Zwrite: signature */
zoptions.flags |= ZWRITE_OPT_SIGNATURE;
zoptions.signature = optarg;
break;
case 'f':
/* Zwrite: file system specification (ignored) */
break;
case 'm':
/* Message on rest of line */
messageflag = TRUE;
break;
case '?':
error = TRUE;
break;
}
if (error || messageflag) break;
}
if (class != NULL || instance != NULL) {
zephyr = TRUE;
}
if (messageflag) {
zoptions.flags |= ZCRYPT_OPT_MESSAGE;
zoptions.message = BuildArgString(argv, optind, argc);
if (!zoptions.message)
{
printf("Memory allocation error.\n");
error = TRUE;
}
} else if (optind < argc) {
error = TRUE;
}
if (mode == M_NONE) mode = (zephyr?M_ZEPHYR_ENCRYPT:M_ENCRYPT);
if (mode == M_ZEPHYR_ENCRYPT && !zephyr) error = TRUE;
if (!error && fname == NULL && (class != NULL || instance != NULL)) {
fname = GetZephyrVarKeyFile(argv[0], class, instance);
}
if (error || fname == NULL) {
printf("Usage: %s [-Z|-D|-E|-R|-S] [-F Keyfile] [-c class] [-i instance]\n", argv[0]);
printf(" [-advqtluon] [-s signature] [-f arg] [-m message]\n");
printf(" One or more of class, instance, and keyfile must be specified.\n");
} else {
if (mode == M_RANDOMIZE) {
/* Choose a new, random key */
/*
FILE *fkey = fopen(fname, "w");
if (!fkey)
printf("Could not open key file for writing: %s\n", fname);
else
{
char string[100];
fputs(fkey, string);
fclose(fkey);
}
*/
printf("Feature not yet implemented.\n");
} else if (mode == M_SETKEY) {
/* Set a new, user-entered key */
char newkey[MAX_KEY];
FILE *fkey;
if (isatty(0)) {
printf("Enter new key: ");
/* Really should read without echo!!! */
fgets(newkey, MAX_KEY - 1, stdin);
} else {
fgets(newkey, MAX_KEY - 1, stdin);
}
fkey = fopen(fname, "w");
if (!fkey) {
printf("Could not open key file for writing: %s\n", fname);
} else {
if (fputs(newkey, fkey) != strlen(newkey) || putc('\n', fkey) != '\n') {
printf("Error writing to key file.\n");
fclose(fkey);
} else {
fclose(fkey);
printf("Key update complete.\n");
}
}
} else {
/* Encrypt/decrypt */
FILE *fkey = fopen(fname, "r");
if (!fkey) {
printf("Could not open key file: %s\n", fname);
} else {
char keystring[MAX_KEY];
fgets(keystring, MAX_KEY-1, fkey);
if (mode == M_ZEPHYR_ENCRYPT || mode == M_ENCRYPT) {
do_encrypt(keystring, (mode == M_ZEPHYR_ENCRYPT), class, instance,
&zoptions, fname);
} else {
do_decrypt(keystring);
}
fclose(fkey);
}
}
}
/* Always print the **END** message if -D is specified. */
if (mode == M_DECRYPT) printf("**END**\n");
exit(0);
}
/* The 'owl_zcrypt_decrypt' function was written by kretch for Owl.
* Decrypt the message in 'in' on class 'class' and instance
* 'instance' and leave the result in 'out'. Out must be a buffer
* allocated by the caller.
*
* return 0 on success, otherwise -1
*/
int owl_zcrypt_decrypt(char *out, char *in, char *class, char *instance) {
char *fname, keystring[MAX_KEY], *inptr, *endptr;
FILE *fkey;
des_cblock key;
des_key_schedule schedule;
char input[8], output[9];
int i, c1, c2;
fname=GetZephyrVarKeyFile("zcrypt", class, instance);
if (!fname) return(-1);
fkey=fopen(fname, "r");
if (!fkey) return(-1);
fgets(keystring, MAX_KEY-1, fkey);
fclose(fkey);
strcpy(out, "");
output[0] = '\0'; /* In case no message at all */
output[8] = '\0'; /* NULL at end will limit string length to 8 */
des_string_to_key(keystring, key);
des_key_sched(key, schedule);
inptr=in;
endptr=in+strlen(in)-1;
while (inptr<endptr) {
for (i=0; i<8; i++) {
c1=(inptr[0])-BASE_CODE;
c2=(inptr[1])-BASE_CODE;
input[i]=c1 * 0x10 + c2;
inptr+=2;
}
des_ecb_encrypt(input, output, schedule, FALSE);
strcat(out, output);
}
if (output[0]) {
if (output[strlen(output)-1] != '\n') {
strcat(out, "\n");
}
} else {
strcat(out, "\n");
}
return(0);
}
int owl_zcrypt_encrypt(char *out, char *in, char *class, char *instance) {
/* static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) { */
char *fname, keystring[MAX_KEY];
FILE *fkey;
des_cblock key;
des_key_schedule schedule;
char input[8], output[8];
int size, length, i;
char *inbuff = NULL, *inptr;
int use_buffer = FALSE;
int num_blocks=0, last_block_size=0;
fname=GetZephyrVarKeyFile("zcrypt", class, instance);
if (!fname) return(-1);
fkey=fopen(fname, "r");
if (!fkey) return(-1);
fgets(keystring, MAX_KEY-1, fkey);
fclose(fkey);
des_string_to_key(keystring, key);
des_key_sched(key, schedule);
inbuff=in;
length=strlen(inbuff);
num_blocks=(length+7)/8;
last_block_size=((length+7)%8)+1;
use_buffer=TRUE;
strcpy(out, "");
inptr=inbuff;
while (TRUE) {
/* Get 8 bytes from buffer */
if (num_blocks > 1) {
size = 8;
memcpy(input, inptr, size);
inptr+=8;
num_blocks--;
} else if (num_blocks == 1) {
size=last_block_size;
memcpy(input, inptr, size);
num_blocks--;
} else {
size=0;
}
/* Check for EOF and pad the string to 8 chars, if needed */
if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */
if (size<8) memset(input + size, 0, 8 - size);
/* Encrypt and output the block */
des_ecb_encrypt(input, output, schedule, TRUE);
for (i = 0; i < 8; i++) {
sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE);
sprintf(out + strlen(out), "%c", (output[i] & 0x0f) + BASE_CODE);
}
if (size < 8) break;
}
return(0);
}
/* Build a space-separated string from argv from elements between start *
* and end - 1. malloc()'s the returned string. */
char *BuildArgString(char **argv, int start, int end) {
int len = 1;
int i;
char *result;
/* Compute the length of the string. (Plus 1 or 2) */
for (i = start; i < end; i++) {
len += strlen(argv[i]) + 1;
}
/* Allocate memory */
result = (char *)malloc(len);
if (result) {
/* Build the string */
char *ptr = result;
/* Start with an empty string, in case nothing is copied. */
*ptr = '\0';
/* Copy the arguments */
for (i = start; i < end; i++) {
char *temp = argv[i];
/* Add a space, if not the first argument */
if (i != start) *ptr++ = ' ';
/* Copy argv[i], leaving ptr pointing to the '\0' copied from temp */
while ((*ptr = *temp++)!=0) {
ptr++;
}
}
}
return(result);
}
#define MAX_BUFF 258
#define MAX_SEARCH 3
/* Find the class/instance in the .crypt-table */
char *GetZephyrVarKeyFile(char *whoami, char *class, char *instance) {
char *keyfile = NULL;
char *varname[MAX_SEARCH];
int length[MAX_SEARCH], i;
char buffer[MAX_BUFF];
char *filename;
char result[MAX_SEARCH][MAX_BUFF];
int numsearch = 0;
FILE *fsearch;
memset(varname, 0, sizeof(varname));
/* Determine names to look for in .crypt-table */
if (instance) {
varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance);
}
if (class) {
varname[numsearch++] = owl_sprintf("crypt-%s:", class);
}
varname[numsearch++] = owl_strdup("crypt-default:");
/* Setup the result array, and determine string lengths */
for (i = 0; i < numsearch; i++) {
result[i][0] = '\0';
length[i] = strlen(varname[i]);
}
/* Open~/.crypt-table */
filename = owl_sprintf("%s/.crypt-table", getenv("HOME"));
fsearch = fopen(filename, "r");
if (fsearch) {
/* Scan file for a match */
while (!feof(fsearch)) {
fgets(buffer, MAX_BUFF - 3, fsearch);
for (i = 0; i < numsearch; i++) {
if (strncasecmp(varname[i], buffer, length[i]) == 0) {
int j;
for (j = length[i]; buffer[j] == ' '; j++)
;
strcpy(result[i], &buffer[j]);
if (*result[i]) {
if (result[i][strlen(result[i])-1] == '\n') {
result[i][strlen(result[i])-1] = '\0';
}
}
}
}
}
/* Pick the "best" match found */
keyfile = NULL;
for (i = 0; i < numsearch; i++) {
if (*result[i]) {
keyfile = result[i];
break;
}
}
if (keyfile == NULL) {
/* printf("Could not find key table entry.\n"); */
} else {
/* Prepare result to be returned */
char *temp = keyfile;
keyfile = (char *)malloc(strlen(temp) + 1);
if (keyfile) {
strcpy(keyfile, temp);
} else {
/* printf("Memory allocation error.\n"); */
}
}
fclose(fsearch);
} else {
/* printf("Could not open key table file: %s\n", filename); */
}
for(i = 0; i < MAX_SEARCH; i++) {
owl_free(varname[i]);
}
owl_free(filename);
return(keyfile);
}
static pid_t zephyrpipe_pid = 0;
/* Open a pipe to zwrite */
static FILE *GetZephyrPipe(char *class, char *instance, ZWRITEOPTIONS *zoptions) {
int fildes[2];
pid_t pid;
FILE *result;
char *argv[20];
int argc = 0;
if (pipe(fildes) < 0) return(NULL);
pid = fork();
if (pid < 0) {
/* Error: clean up */
close(fildes[0]);
close(fildes[1]);
result = NULL;
} else if (pid == 0) {
/* Setup child process */
argv[argc++] = "zwrite";
argv[argc++] = "-n"; /* Always send without ping */
if (class) {
argv[argc++] = "-c";
argv[argc++] = class;
}
if (instance) {
argv[argc++] = "-i";
argv[argc++] = instance;
}
if (zoptions->flags & ZWRITE_OPT_NOAUTH) argv[argc++] = "-d";
if (zoptions->flags & ZWRITE_OPT_QUIET) argv[argc++] = "-q";
if (zoptions->flags & ZWRITE_OPT_VERBOSE) argv[argc++] = "-v";
if (zoptions->flags & ZWRITE_OPT_SIGNATURE) {
argv[argc++] = "-s";
argv[argc++] = zoptions->signature;
}
argv[argc++] = "-O";
argv[argc++] = "crypt";
argv[argc] = NULL;
close(fildes[1]);
if (fildes[0] != STDIN_FILENO) {
if (dup2(fildes[0], STDIN_FILENO) != STDIN_FILENO) exit(0);
close(fildes[0]);
}
close(fildes[0]);
execvp(argv[0], argv);
printf("Exec error: could not run zwrite\n");
exit(0);
} else {
close(fildes[0]);
/* Create a FILE * for the zwrite pipe */
result = (FILE *)fdopen(fildes[1], "w");
zephyrpipe_pid = pid;
}
return(result);
}
/* Close the pipe to zwrite */
void CloseZephyrPipe(FILE *pipe) {
fclose(pipe);
waitpid(zephyrpipe_pid, NULL, 0);
zephyrpipe_pid = 0;
}
#define MAX_RESULT 2048
void block_to_ascii(char *output, FILE *outfile) {
int i;
for (i = 0; i < 8; i++) {
putc(((output[i] & 0xf0) >> 4) + BASE_CODE, outfile);
putc( (output[i] & 0x0f) + BASE_CODE, outfile);
}
}
#define MAX_LINE 128
/* Encrypt stdin, with prompt if isatty, and send to stdout, or to zwrite
if zephyr is set. */
static int do_encrypt(char *keystring, int zephyr, char *class, char *instance, ZWRITEOPTIONS *zoptions, char* keyfile) {
des_cblock key;
des_key_schedule schedule;
char input[8], output[8];
int size;
FILE *outfile = stdout;
int error = FALSE;
char *inbuff = NULL, *inptr;
int freein = FALSE;
int use_buffer = FALSE;
int num_blocks=0, last_block_size=0;
des_string_to_key(keystring, key);
des_key_sched(key, schedule);
if (zephyr) {
if (zoptions->flags & ZCRYPT_OPT_MESSAGE) {
/* Use the -m message */
int length;
inbuff = zoptions->message;
length = strlen(inbuff);
num_blocks = (length + 7) / 8;
last_block_size = ((length + 7) % 8) + 1;
use_buffer = TRUE;
} else if (isatty(0)) {
/* tty input, so show the "Type your message now..." message */
if (zoptions->flags & ZCRYPT_OPT_IGNOREDOT) {
printf("Type your message now. End with the end-of-file character.\n");
} else {
printf("Type your message now. End with control-D or a dot on a line by itself.\n");
}
use_buffer = TRUE;
if ((inptr = inbuff = (char *)malloc(MAX_RESULT)) == NULL) {
printf("Memory allocation error\n");
return FALSE;
}
while (inptr - inbuff < MAX_RESULT - MAX_LINE - 20) {
fgets(inptr, MAX_LINE, stdin);
if (inptr[0]) {
if (inptr[0] == '.' && inptr[1] == '\n' &&
!(zoptions->flags & ZCRYPT_OPT_IGNOREDOT)) {
inptr[0] = '\0';
break;
} else {
inptr += strlen(inptr);
}
} else {
break;
}
}
num_blocks = (inptr - inbuff + 7) / 8;
last_block_size = ((inptr - inbuff + 7) % 8) + 1;
freein = TRUE;
}
/* if (zephyr) */
outfile = GetZephyrPipe(class, instance, zoptions);
if (!outfile) {
printf("Could not run zwrite\n");
if (freein && inbuff) {
free(inbuff);
}
return(FALSE);
}
}
inptr = inbuff;
/* Encrypt the input (inbuff or stdin) and send it to outfile */
while (TRUE) {
if (use_buffer) {
/* Get 8 bytes from buffer */
if (num_blocks > 1) {
size = 8;
memcpy(input, inptr, size);
inptr += 8;
num_blocks--;
} else if (num_blocks == 1) {
size = last_block_size;
memcpy(input, inptr, size);
num_blocks--;
} else {
size = 0;
}
} else {
/* Get 8 bytes from stdin */
size = fread(input, 1, 8, stdin);
}
/* Check for EOF and pad the string to 8 chars, if needed */
if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */
if (size < 8) memset(input + size, 0, 8 - size);
/* Encrypt and output the block */
des_ecb_encrypt(input, output, schedule, TRUE);
block_to_ascii(output, outfile);
if (size < 8) break;
}
/* Close out the output */
if (!error) putc('\n', outfile);
if (zephyr) CloseZephyrPipe(outfile);
/* Free the input buffer, if necessary */
if (freein && inbuff) free(inbuff);
return(!error);
}
/* Read a half-byte from stdin, skipping invalid characters. Returns -1
if at EOF or file error */
int read_ascii_nybble() {
char c;
while (TRUE) {
if (fread(&c, 1, 1, stdin) == 0) {
return(-1);
} else if (c >= BASE_CODE && c <= LAST_CODE) {
return(c - BASE_CODE);
}
}
}
/* Read both halves of the byte and return the single byte. Returns -1
if at EOF or file error. */
int read_ascii_byte() {
int c1, c2;
c1 = read_ascii_nybble();
if (c1 >= 0) {
c2 = read_ascii_nybble();
if (c2 >= 0) {
return c1 * 0x10 + c2;
}
}
return(-1);
}
/* Read an 8-byte DES block from stdin */
int read_ascii_block(char *input) {
int c, i;
for (i = 0; i < 8; i++) {
c = read_ascii_byte();
if (c < 0) return(FALSE);
input[i] = c;
}
return(TRUE);
}
/* Decrypt stdin */
int do_decrypt(char *keystring) {
des_cblock key;
des_key_schedule schedule;
char input[8], output[9];
output[0] = '\0'; /* In case no message at all */
output[8] = '\0'; /* NULL at end will limit string length to 8 */
des_string_to_key(keystring, key);
des_key_sched(key, schedule);
while (read_ascii_block(input)) {
des_ecb_encrypt(input, output, schedule, FALSE);
printf("%s", output);
}
if (output[0]) {
if (output[strlen(output)-1] != '\n') {
printf("\n");
}
} else {
printf("\n");
}
return(0);
}
#endif
|